Wednesday, 12 November 2014

web.xml servlet configuration

In this chapter I will explain about deploy descriptor file (web.xml), as I mention in previous chapter that deployment descriptor file is per application and contain the configuration information of servlet, and servlet container read this file and behave accordingly. web.xml file present inside WEB-INF folder.

Root element/tag of web.xml file is web-app. It denote one web application,where all the web related configuration present. See below to understand-

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaeexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0" metadata-complete="true">
  3.  ..............................
  4. .....................................
  5. </web-app>

Inside web-app normally we declare the servlet configuration. Below the <servlet> element declares the HelloServlet, the examples.Hello Java class implements the servlet, and the <servlet-mapping> element specifies the /hello URL pattern that invokes the servlet in a browser. This URL pattern is used in the index.html file.



    <display-name>HelloWorld Application</display-name>
    <description>
        This is a simple web application
    </description>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>examples.Hello</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>


Sometime we need to pass some parameter to servlet so in this case 
we can use <init-param> element. see below servlet- 



<servlet>
        <servlet-name>Servlet</servlet-name>
         <servlet-class>mysite.server.TeamServlet</servlet-class> 
          <init-param>            
              <param-name>bgColor</param-name>              
              <param-value>#CC0000</param-value>         
          </init-param>  
 </servlet>


 If user wants to show some page when user try to hit simply domain name rather than full qualified path of resource then in this case we can open by default page i.e. welcome file list- and from welcome file user can navigate to other page

<welcome-file-list> 
    <welcome-file>index</welcome-file> 
</welcome-file-list>
 
If we are using filters in our application (filter already described in previous chapter). so in this case-
filter tag is required see below-


<filter>
        <filter-name>logSpecial</filter-name>
        <filter-class>mysite.server.LogFilterImpl</filter-class>
        <init-param>
            <param-name>logType</param-name>
            <param-value>special</param-value>
        </init-param>
</filter>      
 
<filter-mapping>
        <filter-name>logSpecial</filter-name>
        <url-pattern>*.special</url-pattern>
</filter-mapping> 
 
 
 If user want to handle error then error tag is required. see below -

<error-page>
        <error-code>500</error-code>
        <location>/errors/servererror.jsp</location>
</error-page>
 
If user want to pass application related parameter then context-param 
can be used.This should be used outside of servlet. see below-
 

<context-param>  
       <param-name>bgColor</param-name> 
        <param-value>#CC0000</param-value>  
</context-param>
 

If we are using listener in the application then we need to register this first by using listener tag. see below-

<listener>
    <listener-class>com.rkclass.listener.MyContextListener</listener-class>
</listener>

Some more tags is there but I will explain them in advance chapter.

Thanks !!!

No comments:

Post a Comment