Learn Basics of Sample web.xml in java


What is Web.xml in java?



Web.xml is web deployment descriptor used for any web application written in java. Every web application
that is developed in java should have this xml file in Root-Folder/WEB-INF.Web.xml is defined as part java specification. so every vendors like tomcat, jboss or other app  servers should have to implement in their own webbased frameworks.web.xml contains all the jsp,servlet configuration, filter, listener configurations.

Mostly these days developer uses eclipse for web.xml modifications. This validates xml file against schema and gives warning if any


web.xml configuration:-


1.xml version:-

<?xml version="1.0" encoding="UTF-8"?>

The above line is starting line for any xml file which contains version and encoding type of xml.

2.Root element :-


<web-app xmlns="namespace" xmlns:xsi="schema instance"
          xsi:schemaLocation="schema location"
          version="2.5">


This is high level root element which has  xml namespace(xmlns) to identify the unique name for this xml.
As you know each xml should be validate against either DTD or schema. validation means for each tag, what are the sub elements(display-name) and how many sub elements i.e one or two
Version is servlet specification version for the web.xml.

3. Application metadata


<display-name>name of the application</display-name>
<description> short description </description>

4. Context parameter configuration


display-name represents the name of the web application.
description configured to define the short description of your module

<context-param>
<param-name>conext initialization parameter</param-name>
<param-value>conext initialization value</param-value>
</context-param>
context-param  is used to configure the key and value pair of values in web application.
This values are available through the webapplicaiton. We can configure many context-param values for web applicaiton
5.Listener configuration:-


<listener>
<listener-class>Listener java class</listener-class>
</listener>
This listener entry is used to configure the listeners in web application

5. Servlet configuration :-


<servlet>
 <servlet-name>name</servlet-name>
   <servlet-class>java servlet class with package</servlet-class>
      <load-on-startup>1</load-on-startup>
       <init-param>
   <param-name>init param</param-name>
     <param-value>init param value</param-value>
   </init-param>
  </servlet>
servlet entry configures the servlet defined for any webapplicaiton with name and the servlet class.
load-on-startup is used to pre loading of a servlet. Preloading means servlet will be loaded into web container when applicaiton is started  and calls the callback methods of init methods.
we can specify the integer number which is priority order given to load when multiple servlets are presented in servlet container.

init-param is like context-param  to define the key and values. These values are avilable to specific servlet only unlike context-param in whole applicaitln
<servlet-mapping>
     <servlet-name>name</servlet-name>
 <url-pattern>*.html</url-pattern>
  </servlet-mapping>
servlet-mapping defie the mappings for the defined servlet name in above step to specific url pattern.
servlet name will be called for the request url that ends with html extension.


6. jsp configuration in web.xml :-


<servlet>
 <servlet-name>jsppage</servlet-name>
   <jsp-file>jspdemo.jsp</jsp-file>
   <init-param>
   <param-name>init param</param-name>
     <param-value>init parameter value</param-value>
   </init-param>
  </servlet>

jsp can also be declared like servlet except as seen the above code
and the mapping for the jsp page is as follows

<servlet-mapping>
     <servlet-name>jpspage</servlet-name>
 <url-pattern>/myjsp</url-pattern>
  </servlet-mapping>

servlet-mapping defie the mappings for the defined jsp name in above step to specific url pattern.
jsp name will be called for the request url that ends with html extension.


6. Filters name defination and mapping:-


<filter>
  <filter-name>FilterName</filter-name>
    <filter-class>com.cloudhadoop.FilterClass</filter-class>           
    </filter>
  <filter-mapping>
   <filter-name>FilterName</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>

Filters are called before servlets. Mosty these are used for security purpose like login security implementaiton for checking authrozations and authentication
Filter-mapping is used to define the filter class to specific url patter.
These are the basic of configurations in web.xml. This configurations are same for tomcat or jboss.


Sample web.xml file:-
<web-app>
    <display-name>cloudhadoop</display-name>
    <description>
	 sample web.xml for reference
    </description>
    <context-param>
      <param-name>context parameter as key</param-name>
      <param-value>Context parameter value</param-value>
    </context-param>
    <servlet>
      <servlet-name>servletName</servlet-name>
      <servlet-class>com.cloudhadoop.ServletClass</servlet-class>
      <init-param>
        <param-name>key</param-name>
        <param-value>value</param-value>
      </init-param>
    </servlet>
     <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list>

</web-app>
Points to remember:-
1.The order all the elements should be as per Schema
2.For every start tag, there must be end tag.




No comments:

Post a Comment

Note: only a member of this blog may post a comment.