java2db

How to configure a servlet in web.xml (Deployment Descriptor)



The web.xml file is used to describe the details of certain resource . The container will recognize that particular resource by using this file.If any servlet based web application has been deployed ,the  servlet container reads the web.xml (Deployment descriptor) file.

According to java Servlet specification, each web application must have a web.xml file.

 

How to configure servlet in web.xml Rules that are follow to write a XML (Extensible Markup Language) file is given below:

How to configure servlet in web.xml  The XML declaration,elements and attributes are case sensitive.

Declaration: <?xml> and <?XML> are not the same.
Element : <servlet> and <Servlet> not the same.

How to configure servlet in web.xml  XML declaration must be in the first line in the XML document.not even white space or comments may appear before it.

How to configure servlet in web.xml  An XML document must have one root element.
Examle: <web-app>

How to configure servlet in web.xml  XML element stars eith less-than (<) and ends with greater-than (>).

How to configure servlet in web.xml  An element consists of a start tag, possibly followed by text , followed by an end tag.
Example : <country>Bharath</country>
How to configure servlet in web.xml  It allows element inside an element.
Example : <p><country>Bharath</country></p>

 

How to configure servlet in web.xml  Example to configure a servlet in web.xml :

 <?xml version=”1.0″ encoding=”UTF-8″?> Delaration with current xml version and character encoding type.

 <web-app> is the root element .

 <servlet> tag contains the declarative data of a servlet.

 <servlet-name> defines the logical name of the servlet.

 <servlet-class> is the fully-qualified class name of the servlet .

 <servlet-mapping> defines the mapping between the servlet and a URL pattern.

 <url-pattern> contains a pattern to locate the servlet.It takes a string that starts with / or * .

 

Other tags that are using in the <servlet>  are listed below.

 Tag Type Use of the Tag
<icon> Optional Contains a small-icon and large-icon element.
<display-name> Optional A short name intended to be displayed by GUI tools.
<jsp-file> Optional The full path to a JSP file within the Web application.
<init-param> Optional Contains a name/value pair as an initialization attribute of the servlet.
<load-on-startup> Optional Load the servlet during deployment or server startup.
<run-as> Optional Specifies the run-as identity used for the web Application execution.
<security-role-ref> Optional Used to link a security role name defined by <security-role>.
<description> Optional A text description of the servlet.

 

Configure a servlet in web.xml with <welcome-file-list>  :

The index.jsp file will load as a welcome file,when you call the url.

 

 Configure a servlet in web.xml with Multiple <url-pattern> :

We can configure multiple url patterns to a servlet in the <servlet-mapping>.

 

 The below tags and it’s sub-tags are using in web.xml for servlet web applications.

 <error-page>

 Tag Type Use of the Tag
<error-code> Optional A valid HTTP error code, for example, 404.
<exception-type> Optional A fully-qualified class name of a Java exception type,Ex:java.lang.String()
<location> Required The location of the resource(file path) to display in response to the error.

 <listener>

 Tag Type Use of the Tag
<listener-class> Optional Name of the class that responds to a Web application event.

 <taglib>

 Tag Type Use of the Tag
<taglib-location> Optional Gives the file name of the tag library descriptor relative to the root of the Web application.
<taglib-uri> Optional Describes a URI, relative to the location of the web.xml document, identifying a Tag Library used in the Web application.

 <filter>

 Tag Type Use of the Tag
<icon> Optional Contains a small-icon and large-icon element.
<filter-name> Required Used to reference the filter definition elsewhere in the deployment descriptor.
<display-name> Optional A short name intended to be displayed by GUI tools.
<description> Optional A text description of the filter.
<filter-class> Required The fully-qualified class name of the filter.
<init-param> Optional Contains a name/value pair as an initialization attribute of the filter.

 <filter-mapping>

 Tag Type Use of the Tag
<filter-name> Required  The name of the filter to which you are mapping a URL pattern or servlet.
<url-pattern> Required Describes a pattern used to resolve URLs.

 

 




Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Request attributes in servlet with an example ( Attribute scope )
Session attributes in servlet with example (Attribute scope)
ServletConext Attributes in servlet ( Attribute scope )
   [will not be published]



^ <