web.xml structure

Available

7.1 General schema of web.xml

The web.xml file stores application configuration information. It is not a mandatory part of it, but it is very widely used to configure a web application.

This file must be located in the WEB-INF folder . When Tomcat starts up, it reads its contents and uses the configuration it contains. If the file contains errors, then Tomcat also displays an error.

Example web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">

  <servlet>
       <servlet-name>HelloWorld</servlet-name>
       <servlet-class>HelloServlet</servlet-class>
   </servlet>

  <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>/welcome</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
  </welcome-file-list>

</web-app>

"HelloWorld"The mapping of the servlet name and the servlet class is written here in green"HelloServlet" . The mapping of the servlet name and the URL chunk is written in blue"HelloWorld""http://localhost/welcome" . Thus, it says here that when accessing the /welcome path, you need to call the servlet HelloServlet.class.

The red color indicates the file that needs to be given upon request http://localhost/- this is the so-called welcome page . If the user simply types in the browser the name corresponding to the root of our web application, then the contents of the index.html.

7.2 servlet, servlet-mapping

One servlet can serve requests at different URLs, so in web-xml, the servlet and its mapping to URLs are written separately. First, we describe the servlets, giving each a unique string name, and then we specify how each servlet maps to which url.

Example web.xml:

<web-app>

  <servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>com.codegym.RemotingServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>restapi</servlet-name>
    <servlet-class>com.codegym.RestApiServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>restapi</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

</web-app>

In this example, two servlets are declared, and each is mapped to a different url template. The servlet RemotingServletserves all requests that go to/remoting/* . The servlet RestApiServletserves all requests that go to/api/* . Servlets also have the order from loading - the load-on-startup parameter.

7.3 Servlet Options

With the help of web.xml, parameters can be passed to the servlet during its initialization, they will be available through the interface ServletConfig. You can also set parameters for the entire web application, they will be available through the ServletContext.

Example web.xml:

<web-app>
  <context-param>
     <description>Server production mode</description>
     <param-name>productionMode</param-name>
     <param-value>false</param-value>
  </context-param>

  <context-param>
     <param-name>appPropertiesConfig</param-name>
     <param-value>
        classpath:local-app.properties
        classpath:web-app.properties
     </param-value>
  </context-param>

  <servlet>
     <servlet-name>mainservlet</servlet-name>
     <servlet-class>com.codegym.ApplicationServlet</servlet-class>
     <init-param>
        <param-name>application</param-name>
        <param-value>com.codegym.App</param-value>
     </init-param>
     <init-param>
        <param-name>widgetset</param-name>
        <param-value>com.codegym.WidgetSet</param-value>
     </init-param>
     <init-param>
        <param-name>ui</param-name>
        <param-value>com.codegym.AppUI</param-value>
     </init-param>
  </servlet>
</web-app>

The code highlighted in green is where we set the parameters forServletContext . There are two of them:

  • productionModewith value false
  • appPropertiesConfigwith an array of two strings:
    • classpath:local-app.properties
    • classpath:web-app.properties

The parameters for the servlet are indicated in blueApplicationServlet , they will be available to it through ServletConfig:

  • applicationwith value com.codegym.App
  • widgetsetwith value com.codegym.WidgetSet
  • uiwith value com.codegym.AppUI

7.4 filter, filter-mapping

The web application may also contain special utility servlets - filters. They perform various service tasks: redirect calls, check authorization, etc.

Example web.xml:

<web-app>

  <servlet>
      <servlet-name>remoting</servlet-name>
      <servlet-class>RemotingServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>remoting </servlet-name>
      <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>

  <filter>
      <filter-name>total_filter</filter-name>
      <filter-class>com.javrush.TotalFilter</filter-class>
  </filter>

  <filter-mapping>
      <filter-name>total_filter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

Before the request reaches the servlet RemotingServlet, it will be processed by the filter TotalFiler. This filter is configured to intercept all requests that go to our web application. This is clearly hinted at by the url template it is mapped to: /*.

You will read more about servlets and filters in the following lectures.

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet