Wednesday, 25 June 2014

Understanding Web.xml Files

Tomcat uses a web.xml file in the WEB-INF directory of an application to configure the website.

This was inspired by reading material on http://wiki.metawerx.net/wiki/web.xml and analysing the VfB web.xml file.

The order of tags should be respected.

The minimal tags are:

<?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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
</web-app>

we can then put the following blocks of XML tags into the <web-app> tag:

Web-App Name
<display-name>My First WebApp</display-name>
Name of the web-app - appears in the manager with this name.

Description
<description>The first web-app I created</description>
Description of what the web-app does

Clusters
<distributable />
Add if the app can be used on computer clusters

Session Time-Out
<session-config>
    <session-timeout>120</session-timeout>
</session-config>
Set session info, such as time-out (here 120 minutes).

JSP (Java Server Pages) Configuration
<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <include-prelude>/WEB-INF/jspf/prelude1.jspf</include-prelude>
        <include-coda>/WEB-INF/jspf/coda1.jspf</include-coda>
     </jsp-property-group>
</jsp-config>
Configure the Java Server Pages: Here a header and footer jsp script is added to each .jsp file by using the  jsp-property-group tag, specifying the url-pattern as all pages ending in .jsp and using the include-prelude and include-coda tags respectively.

Context (Environment variables)
You can specify variables in web.xml in order to refer to them in the rest of the application, a bit like environment variables.  e.g.
<context-param>
    <param-name>myImportantEnvironmentInteger</param-name>
    <param-value>100</param-value>
</context-param>

Servlet
A Java servlet is a java program that usually serves up HTML when requested by an external user, thus we need to tell the web-app how the user calls it.  c.f. a JSP file that is similar but an HTML file with (usually small amounts of) java in it in tags - a bit like PHP.
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <description>This is a servlet that implements the Spring Framework</description>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> /WEB-INF/classes/springmvc-servlet.xml </param-value>
    </init-param>
</servlet>
We specify the servlets (usually just the ones directly callable by users entering web addresses).  They require a name for use in this web-xml (servlet-name) and the address of the class file where they are kept (servlet-class).  The init() method will be called when the application starts as the load-on-startup tag is specified.  The number determines the order that the servlet is loaded in on start-up.  The parameters required for loading can be passed to it by using an init-param tag for each parameter.

Servlet Calling (Mapping)
Add this code after a servlet has been defined
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
Simply process all requests for HTML files through the springmvc servlet (an external framework made in java servlet form).  In general servlet-mapping maps external user specified web-addresses to java servlet programs.

Listeners
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Listeners are run when certain actions occur in the application e.g. session expiry etc.  They need to be specified here as they need to run when the server is running not when called by a user.

Filters
<filter>
    <filter-name>charsetFilter</filter-name>
    <filter-class>uk.ac.ed.vfb.servlets.CharsetEncodingFilter</filter-class>
</filter>
Filters are extra java programs that are run when the user specifies certain pages, and are generally external to the application.

Filter Mapping
<filter-mapping>
    <filter-name>charsetFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
For them to be called by users in addresses we need to map them as with servlets.  E.g. the external program charsetFilter is run on all pages.

Error Handling
<error-page>
    <error-code>404</error-code>
    <location>/error.htm</location>
</error-page>
Map errors based on the server error code to a certain page specified in the location tag.


No comments:

Post a Comment