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"

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

<param-name>conext initialization parameter</param-name>
<param-value>conext initialization value</param-value>
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-class>Listener java class</listener-class>
This listener entry is used to configure the listeners in web application

5. Servlet configuration :-

   <servlet-class>java servlet class with package</servlet-class>
   <param-name>init param</param-name>
     <param-value>init param value</param-value>
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 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 :-

   <param-name>init param</param-name>
     <param-value>init parameter value</param-value>

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 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:-


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:-
	 sample web.xml for reference
      <param-name>context parameter as key</param-name>
      <param-value>Context parameter value</param-value>

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.