Configuring Web Application Defaults with web.xml

Every Servlet 2.4 Web application must contain a web.xml deployment descriptor. This file must be placed in the WEB-INF directory of the Web application.

However, Tomcat comes with a default web.xml in CATALINA_HOME/conf. This file is similar to a Web application’s web.xml file but is used to specify the default properties for all Web applications that are running within this server instance.

To gain an understanding of what you can do with this file, let’s look at it. The file starts with the standard XML header and a reference to a DTD. Unlike server.xml, web.xml can be formally validated against a corresponding DTD.

 <?xml version="1.0" encoding="ISO-8859-1"?>  <Web-app xmlns="http://java.sun.com/xml/ns/j2ee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/Web-app_2_4.xsd"      version="2.4"> 

The most noteworthy thing about this is that the default Web application version is 2.4.

Default Servlet Definitions

The default servlet that invokes any resources not mapped to any other servlet, either in this web.xml file or in an application’s web.xml file, is defined in the first <servlet> definition. This includes all static resources. You’ll see the mappings later.

 <web-app>    <servlet>      <servlet-name>default</servlet-name>      <servlet-class>        org.apache.catalina.servlets.DefaultServlet      </servlet-class>      <init-param>        <param-name>debug</param-name>        <param-value>0</param-value>      </init-param>      <init-param>        <param-name>listings</param-name>        <param-value>true</param-value>      </init-param>      <load-on-startup>1</load-on-startup>    </servlet> 

Next comes the invoker servlet, which loads and executes anonymous servlets directly using the servlet’s filename. This mechanism is inherently unsafe, because any class that exists in Tomcat’s classpath can be invoked in this way, so the invoker servlet has been commented out of recent versions of Tomcat.

 <!-- <servlet>    <servlet-name>invoker</servlet-name>    <servlet-class>      org.apache.catalina.servlets.InvokerServlet    </servlet-class>    <init-param>      <param-name>debug</param-name>      <param-value>0</param-value>    </init-param>    <load-on-startup>2</load-on-startup>  </servlet>  --> 

Just as servlets have their default servlet, JSP pages have a servlet that compiles them into servlets and executes them.

 <servlet>    <servlet-name>jsp</servlet-name>    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>    <init-param>      <param-name>fork</param-name>      <param-value>false</param-value>    </init-param>    <init-param>      <param-name>xpoweredBy</param-name>      <param-value>false</param-value>    </init-param>    <load-on-startup>3</load-on-startup>  </servlet> 

The next set of servlets is commented out by default. You should uncomment them if you plan to add Apache-style Server Side Include (SSI) features to the stand-alone Tomcat server or process CGI. You’ll see more of this in later chapters.

Matching URLs: Servlet Mappings

Servlet mappings specify which servlets are to process incoming requests, as defined by the request URL.

 <!-- The mapping for the default servlet -->  <servlet-mapping>    <servlet-name>default</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping> 

The previous <servlet-mapping> element maps the pattern / to the default servlet defined earlier in web.xml. So, www.apress.com/tomcat/ will map to the default servlet, which will process the request.

The second <servlet-mapping> maps all requests that end in /servlet/* to the invoker servlet defined earlier in web.xml.

 <!-- The mapping for the invoker servlet -->  <!-- <servlet-mapping>    <servlet-name>invoker</servlet-name>    <url-pattern>/servlet/*</url-pattern>  </servlet-mapping>  --> 

The next <servlet-mapping> specifies that all URLs containing *.jsp and *.jspx should be passed to the servlet named jsp for processing:

 <!-- The mapping for the JSP servlet -->  <servlet-mapping>    <servlet-name>jsp</servlet-name>    <url-pattern>*.jsp</url-pattern>  </servlet-mapping>  <servlet-mapping>    <servlet-name>jsp</servlet-name>    <url-pattern>*.jspx</url-pattern>  </servlet-mapping> 

Configuring Session Timeout

The <session-config> element configures how long Tomcat will maintain a session on the server side on behalf of a client. For example, if the user leaves a service registration transaction in the middle and doesn’t return to the cart for 30 minutes, all their information will be lost.

You must be careful to balance the <session-timeout> value with the potential of over-loading the server with too many stale sessions.

 <session-config>    <session-timeout>30</session-timeout>  </session-config> 

Configuring Mime Mappings

The <mime-mapping> elements that make up a large chunk of web.xml help Tomcat serve static files with specific extensions to the client. It will generate an HTTP Content-Type header when transmitting the file to the client. Most browsers will use a helper application to process the file being transmitted if it recognizes the Content-Type specified. For example, a browser may start Adobe Acrobat when it detects the application/pdf content type.

   <mime-mapping>      <extension>abs</extension>      <mime-type>audio/x-mpeg</mime-type>    </mime-mapping>  ... and so on ... 

Configuring Welcome Files

To be compatible with the default behavior of most modern Web servers, including Apache, the default servlet will display a welcome file if the incoming URI is terminated in /—for example, http://www.apress.com/.

The default servlet will examine the root directory of the named virtual host and look for index.html, index.htm, or index.jsp in turn to be displayed. Each Web application may override this list in its own deployment descriptor file.

   <welcome-file-list>      <welcome-file>index.html</welcome-file>      <welcome-file>index.htm</welcome-file>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>  </web-app> 



Pro Jakarta Tomcat 5
Pro Apache Tomcat 5/5.5 (Experts Voice in Java)
ISBN: 1590593316
EAN: 2147483647
Year: 2004
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net