Servlet Development and Deployment : Configuring the servlet & Packaging the web application

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
2/20/2012 5:39:52 PM

Configuring the servlet

Before we can deploy our servlet, we need to configure it. All Java EE web applications can be configured via an XML deployment descriptor named web.xml or via annotations. In this section, we will discuss how to configure a Java EE web application via web.xml. The web.xml deployment descriptor for our servlet is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi="" xsi:schemaLocation=
"" version="3.0">

The first few lines are boilerplate XML stating the XML version and encoding, plus the schema used for the XML file and other information. It is safe to just copy and paste these lines and reuse them across applications. The<servlet> and<servlet-mapping> XML tags are used to actually configure our servlet.

The<servlet> tag contains two nested tags:<servlet-name> defines a logical name for the servlet and<servlet-class> indicates the Java class defining the servlet.

The<servlet-mapping> tag also contains two nested tags:<servlet-name> matches the value set inside the<servlet> tag and<url-pattern> sets the URL pattern for which the servlet will execute.

The<url-pattern> tag can be specified in one of the following two ways: by using a path prefix (which is what the previous example does) or by specifying an extension suffix.

Path prefix values for<url-pattern> indicate that any URL paths starting with the given path will be serviced by the corresponding servlet. Path prefix values must start with a forward slash.

Java EE web applications run from within a context root. The context root is the first string in the URL that is neither the server name or IP address nor the port. For example, in the URL http://localhost:8080/simpleapp/simpleservlet, the string simpleapp is the context root. The value for<url-pattern> is relative to the application's context root.

Extension suffix values for<url-pattern> indicate that any URLs ending in the given suffix will be serviced by the corresponding servlet. In the previous example, we chose to use a path prefix. If we had chosen to use an extension suffix, the<servlet-mapping> tag would look something like this:


This would direct any URLs ending with the string .foo to our servlet.

The reason the<servlet-name> tag is specified twice (once inside the<servlet> tag and again inside the<servlet-mapping> tag) is because a Java EE web application can have more than one servlet. Each of the servlets must have a<servlet> tag in the application's web.xml file. The<servlet> tag for each must have a corresponding<servlet-mapping> tag. The<servlet-name> nested tag is used to indicate which<servlet> tag corresponds to which<servlet-mapping> tag.

A Java EE web.xml file can contain many additional XML tags. However, these additional tags are not needed for this simple example. Additional tags will be discussed in future examples when they are needed.

Before we can execute our servlet, we need to package it as part of a web application in a WAR (Web ARchive) file.

Packaging the web application

All Java EE web applications must be packaged in a WAR (Web ARchive) file before they can be deployed. A WAR file is nothing but a compressed file containing our code and configuration. WAR files can be created by any utility that can create files in a ZIP format (for example, WinZip, 7-Zip, and so on). Also, many Java IDEs and build tools such as ANT and Maven automate WAR file creation.

A WAR file must contain the following directories (in addition to its root directory):


  • WEB-INF/classes

  • WEB-INF/lib

The root directory contains JSPs , HTML files, JavaScript files, and CSS files.

WEB-INF contains deployment descriptors such as web.xml.

WEB-INF/classes contains the compiled code (.class files) and may optionally contain property files. Just like with any Java classes, the directory structure must match the package structure. Therefore, this directory typically contains several subdirectories corresponding to the classes contained in it.

WEB-INF/lib contains JAR files containing any library dependencies our code might have.

The root directory, WEB-INF and WEB-INF/classes directories can have subdirectories. Any resources on a subdirectory of the root directory (other than can be accessed by prepending the subdirectory name to its filename. For example, if there was a subdirectory called css containing a CSS file called, this CSS file could be accessed in JSPs and HTML files in the root directory by the following line: WEB-INF) style.css

<link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />

Notice the css prefix to the filename corresponding to the directory where the CSS file resides.

To create a WAR file from scratch, create the previous directory structure in any directory in your system, then perform the following steps:

  1. Copy the web.xml file to WEB-INF.

  2. Create the following directory structure under WEB-INF/classes: net/ensode/glassfishbook/simpleapp.

  3. Copy SimpleServlet.class to the simpleapp directory from the previous step.

  4. From the command line, issue the following command from the directory right above WEB-INF: jar cvf simpleapp.war *.

You should now have a WAR file ready for deployment.

When using Maven to build the code, the WAR file is automatically generated when issuing the mvn package command. The WAR file can be found under the target directory. It is named simpleapp.war.

Before we can execute our application, it needs to be deployed.

Deploying the web application

There are several ways of deploying an application. The easiest and most straightforward way to deploy any Java EE application is to copy the deployment file (in this case, WAR file) to [glassfish installation directory]/glassfish/domains/domain1/autodeploy.

After copying the WAR file to the autodeploy directory, the system log should show a message similar to the following:

_ThreadName=Thread-1;|[AutoDeploy] Successfully autodeployed : /home/heffel/sges-v3/glassfish/domains/domain1/autodeploy/simpleapp.war.|#]

The system log can be found under [glassfish installation directory]/glassfish/domains/domain1/logs/server.log.

The last line should contain the string "Successfully autodeployed", indicating that our WAR file was deployed successfully.

Testing the web application

To verify that the servlet has been properly deployed, we need to point our browser to http://localhost:8080/simpleapp/simpleservlet. After doing so, we should see a page similar to the following:

We mentioned that URL paths for a Java EE application are relative to their context root. The default context root for a WAR file is the name of the WAR file itself (minus the .war extension). As can be seen in the previous screenshot, the context root for our application is simpleapp, which happens to match the name of the WAR file. This default can be changed by adding an additional configuration file to the WEB-INF directory of the WAR file. The name of this file should be sun-web.xml. An example sun-web.xml file that will change the context root of our application from the default simpleapp to simple would look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN"

As can be seen in this example, the context root for the application must be in the<context-root> tag of the sun-web.xml configuration file. After redeploying the file, directing the browser to http://localhost:8080/simple/simpleservlet will execute our servlet. simpleapp.war

The sun-web.xml file can contain a number of additional tags to configure several aspects of the application.

Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
Video Sports
programming4us programming4us