Web Application?

A server like Tomcat is called a servlet container. It is the web application that the servlet container carries and manages. The servlet container must ensure that the web application operates in accordance with Sun's Servlets/JSP Spec.

Components of a web application

The web application in Java consists of the following files.

  • HTML, Images, Style Sheets, JavaScript
  • JSP
  • Servlets
  • Java Classes, Java archive file(jar)
  • web.xml

Some of the above components have a specified location. Servlets and Java classes should be placed in WEB-INF/classes. Java archive files should be placed in WEB-INF/lib. The web.xml file should be placed in WEB-INF.

Web application directory

The default location for web applications in Tomcat is CATALINA_HOME/webapps. If you want to add a new web application, create a subdirectory1 with the appropriate name in CATALINA_HOME/webapps and create the following folder in that subdirectory.

WEB-INF

This directory should contain the web application deployment descriptor, web.xml. Files in the WEB-INF directory can not be accessed directly through a web browser.

WEB-INF/classes

The Java class file (bytecode) should be here.

WEB-INF/lib

The Java archive file (jar) should be here. Note that the same bytecode can be found in the WEB-INF/classes and Java archive files in WEB-INF/lib. In this case, the Tomcat class loader first searches WEB-INF/classes, and loads the classes found in WEB-INF/classes into memory. The class in Java archive file are ignored and can not participate in the program.

Tomcat Class Loader
Tomcat class loader does not refer to the environment variable CLASSPATH. Tomcat class loader looks for classes in the following order:
  1. Java API
  2. WEB-INF/classes directory of a web application
  3. WEB-INF/lib directory of a web application
  4. CATALINA_HOME/lib

web.xml

The heart of the web application is the web.xml file, called the deployment descriptor. A web.xml should be in the WEB-INF/ directory. A web.xml contains all the configuration information on a web application.

Main configuration items in web.xml

  • ServletContext initialization parameter
  • Filter
  • Listener
  • Servlet Definition
  • Servlet Initialization parameters
  • Servlet Mapping
  • Session Configuration
  • welcome file list
  • error page

Example and description of web.xml

When creating the web.xml file, copy CATALINA_HOME/webapps/ROOT/WEB-INF/web.xml file and paste it into WEB-INF / of your web application. Then open the web.xml file and delete everything in the web-app root element. You can configure your web application by adding sub elements of the web-app element, as highlighted below.

web.xml2
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	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_3_0.xsd"
	version="3.0"
	metadata-complete="true">  
	
	<servlet>
		<servlet-name>TestServlet</servlet-name>
		<servlet-class>net.java_school.TestServlet</servlet-class>
		<init-param>
			<param-name>name</param-name>
			<param-value>value</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/Test</url-pattern>
	</servlet-mapping>
	  
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	
</web-app>

The servlet element sets the servlet definition and the initial parameters of the servlet. If the value of the load-on-startup element is 0 or positive, Tomcat creates the servlet object at the start of the web application and calls the init() method to make the service available. The servlet-mapping element is used for servlet-to-URL mapping. As above, if you visit http://localhost:8080/contextPath/Test, the net.java_school.TestServlet servlet will respond. The session-timeout element controls the lifetime of the HttpSession object. If session-timeout is set to 30, the servlet container will destroy the HttpSession object if it has no action for 30 minutes.

Packing

You can use the jar tool (jar.exe) to make your web application a single file. Once development is complete, you can bundle your web application and distribute it to other servlet containers. To do this, you need to create a file with the extension war. (Note that the extension is war rather than jar) If the web application's document base is C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\test, go to the document base and run the following:

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\test>jar cvf test.war .

If you copy this test.war file to the default web application folder of another Tomcat server or another vendor's servlet container, the servlet container will automatically deploy and load the test application.

Comments
  1. This subdirectory is the root directory of the web application to be created. For Tomcat, this directory is called DocumentBase. The DocumentBase and DocumentBase subdirectories (Exclude WEB-INF/) contain static components of the web application. For example, you can place style sheets in the css subdirectory of DocumentBase, JavaScript files in the js subdirectory of DocumentBase, and image files in the images subdirectory of DocumentBase. JSPs can also be placed in the DocumentBase and DocumentBase subdirectories. However, many web developers create a directory like WEB-INF/jsp and place the JSP file there.
  2. To test a Web application that has this web.xml, you need to create a TestServlet. Servlet examples are covered in full in Servlet chapter.
References