Servlet Tutorial

Web.xml in Servlets

web.xml:

  1. Whenever client makes a request to a servlet that request is received by server and server goes to a predefined file called web.xml for the details about a servlet.
  2. web.xml file always gives the details of the servlets which are available in the server.
  3. If the server is not able to find the requested servlet by the client then server generates an error (resource not found) [A resource is a program which resides in server].
  4. If the requested servlet is available in web.xml then server will go to the servlet, executes the servlet and gives response back to client.
webxml

Every web.xml will contain the following entries:

<web-app>
    <servlet>
        <servlet-name>Asha</servlet-name>
        <servlet-class>First</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Asha</servlet-name>
        <url-pattern>Krishna</url-pattern>
    </servlet-mapping>
</web-app>
  • While server is executing web.xml control goes to <url-pattern> of <servlet-mapping> tag. If the requested url and <url-name> web.xml is same then control goes to <servlet- class> tag of <servlet> and takes the <servlet-name> and executes.
  • If the <url-pattern> is not matching, server generates an error.