Web Configuration

Now going to web configuration we can see the two configuration xmls. This will be needed if you have multiple servlets and filters that shares common parameters. You can define only one servlet config xml and put all of contents from both xmls into the one. The tag <context-param> defines parameters that are shared by all servlets and filters whereas <init-param> defines parameters that are accessible by only that <servlet>inside which it is placed. We also defined the DispatcherServlet which is the default request handler for all the requests as defined by the pattern "/" in <url-pattern>.

<web-app>:

  • Defines the root element of the document called Deployment Descriptor

<context-param>:

  • This is an optional element which contains the declaration of a Web application's servlet context initialization parameters available to the entire scope of the web application.
  • The methods for accessing context init parameter is as follows:
    String paramName = getServletContext().getInitParamter("paramName")
    

<init-param>:

  • An optional element within the servlet.
  • Also defines servlet configuration initialization parameters only available to the servlet inside which it's defined.
  • The methods for accessing servlet init parameter is as follows:
    String paramName = getServletConfig().getInitParamter("paramName")
    

<listener>:

  • Defines a listener class to start up and shut down Spring's root application context.
  • We might not use this because we don't define the element <context-param>

<servlet>:

  • Declares a servlet including a refering name, defining class and initialization parameters.
  • We can define multiple servlets in the document the most interesting thing is you can even declare multiple servlets using the same class with different initialization parameters but, the name of each servlet must be unique.

<servlet-mapping>:

  • Maps a URL pattern to a servlet.
  • In this case, all requests will be handled by the DispatcherServlet named dispatcherServlet

web.xml: /webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

results matching ""

    No results matching ""