O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Packaging a Servlet into a WAR File

Problem

You have a servlet and other web resources, and want to package them into a single file for deploying to the server.

Solution

Use jar to make a web archive (WAR) file.

Discussion

Servlets are server-side components for use in web servers, and are discussed in Chapter 18. They can be packaged for easy installation into a web server. A web application in the Servlet API specification is a collection of HTML and/or JSP pages, servlets, and other resources. A typical directory structure might include the following:

index.html, foo.jsp

Web pages

WEB-INF

Server directory

WEB-INF/web.xml

Descriptor

WEB-INF/classes

Directory for servlets and any classes used by them or by JSP

WEB-INF/lib

Directory for any JAR files of classes needed by classes in the WEB-INF/classes directory

Once you have prepared the files in this way, you just package them up with jar :

jar cvf MyWebApp.war .

You then deploy the resulting WAR file into your web server. For details on this, consult the web server documentation.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required