web.xml configuration for servlet

Web.xml configuration for Servlet

 

Configure the servlet

For configuring the servlet need to use <servlet> element. In this we will provide the servlet information such as Servlets name and class under the <servlet-name>, <servlet-class> tags respectively.

Next step is mapping this servlet to the URL or URL pattern with the help of <servlet-mapping>

<?xml version=“1.0”encoding=“UTF-8”?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance “xmlns=“http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”id=“WebApp_ID”version=“3.0”>

<display-name>Testing the servlet</display-name>

       <servlet>

                  <servlet-name>TestServlet</servlet-name>

                  <servlet-class>com.ivv.test.TestServlet</servlet-class>

       </servlet>

       <servlet-mapping>

               <servlet-name>TestServlet</servlet-name>

                <url-pattern>/Test</url-pattern>

         </servlet-mapping>

</web-app>

 

In the mentioned example I have used /Test as URL pattern. We can use any pattern depends on our requirement like

<url-pattern>/Test </url-pattern>
<url-pattern>/Test.do</url-pattern>
<url-pattern>/* </url-pattern> where (*) is wildcard
This mapping will provide the security for the entire application by hiding the necessary information and showing only the relevant info.

web.xml configuration for servlet

web.xml configuration for servlet

Let’s see how this mapping works step by step

  • User entered the URL in the browser http://itzjava.in/Test
  • Then the request comes to the web.xml and checks the tag <url-pattern> for the requested “Test” under the <servlet-mapping> tag.
  • If it matches then it checks the <servlet-name> tag under the same element <servlet-mapping>.
  • With this <servlet-name> value, searches the same value under <servlet> tag, if both matches then it will load the required class in <servlet-class> which will executes the .class file to produce the desired output

Servlet Context Parameters
 
If you want to pass any parameters which are common to all servlets, i.e. for entire application, those parameters declared under   <context-param> tag

<context-param>
         <param-name>appName</param-name>
         <param-value>ALLABOUTJAVA</param-value>
</context-param>

To see more refer this ServletContext example

Servlet Init Parameters

If you want to pass any parameters to Servlet, those parameters can configure in web.xml itself under <servlet> element itself.

<servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.ivv.test.TestServlet</servlet-class>
         <init-param>
                <description>Init Parameters</description>
                <param-name>studentID</param-name>
                <param-value>IVV</param-value>
          </init-param>
</servlet>

Note: These init parameter ‘studentID’ can be accessed by the TestServlet only.

To see more refer this ServletConfig example

Load on Start up.
load-on-startup tag in the servlet gives information when the mentioned servlet has to be loaded by the webcontainer.

<servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.ivv.test.TestServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

To see more refer Load on Startup in web.xml

One thought on “web.xml configuration for servlet

Leave a Reply

Your email address will not be published. Required fields are marked *