Thursday, November 21, 2013

Web.xml

References:-
http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/web_xml.html
This file is used to define each servlet and JSP page inside a web-application.
It is found in WEB-INF directory under the document root of the web application.
It is also called Deployment Descriptor

Context parameters are accessible to any servlet or JSP in the web-app and can be accessed through ServletContext object.
String value = getServletContext().getInitParameter("name_of_context_initialization_parameter");

For springs Context param name for
    application context is  "contextConfigLocation" with value "*.xml"
    log4j is "log4jConfigLocation" with value "*.properties"
    applicationContext.xml is an XML file having the context parameters

Init parameters are specific to a particular servlet and and can be accessed through ServletConfig object.
String value = getServletConfig().getInitParameter("name_of_context_initialization_parameter");

load-on-startup gives the order in which the servlet will initialize

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>The name of the application </display-name>
    <description>Description of Application</description>
    <welcome-file-list>
           <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
     <description>Description of parameter</description>
     <param-name>name_of_context_initialization_parameter</param-name>
     <param-value>value_of_context_initializtion_parameter</param-value>
    </context-param>

 //In case of Servlets , we define our own Servlet classes and corresponding mappings are given  in web.xml

    <servlet>
     <servlet-name>guess_what_name_of_servlet</servlet-name>
     <description>Again, some description</description>
     <servlet-class>com.foo-bar.somepackage.TheServlet</servlet-class>
     <init-param>
       <param-name>foo</param-name>
       <param-value>bar</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
     <servlet-name>name_of_a_servlet</servlet-name>
     <url-pattern>*.some_pattern</url-pattern>
    </servlet-mapping>


 //In case of struts 2 , we define the filters and corresponding mapping of FilterDispatcher in web.xml
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

//In case of springs, we define the servlet and  corresponding mapping of Dispatcher Serv
    <servlet>
        <servlet-name>employee</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>employee</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/employee-servlet.xml</param-value>
    </context-param>
    <session-config>
     <session-timeout>30</session-timeout>
    </session-config>
</web-app>

No comments:

Post a Comment

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...