A Simple WAR File Example
When you create a WAR file, you must include a web.xml file that describes the application. Usually, you need descriptions of at least the various servlets in your application. Listing B.1 shows a crude servlet that calls a JavaServer Page.
Listing B.1 Source Code for ExampleWARServlet.java
package examples.warexample;
import javax.servlet.*; import javax.servlet.http.*;
import java.io.*;
public class ExampleWARServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Just call the JSP RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/ExampleWARJSP.jsp");
dispatcher.forward(request, response); } }
Without a WAR file, you would need to put the ExampleWARJSP.jsp file in your Web server's root directory. Listing B.2 shows ExampleWARJSP.jsp.
Listing B.2 Source Code for ExampleWARJSP.jsp
<html> <body>
<h1>Hello From the WAR!</h1>
<p> This page was invoked by ExampleWARServlet! </body> </html>
The web.xml file for the example application assigns a name to the example servlet and sets up a URL mapping. The mapping is relative to the base path of the installed application. In other words, if the mapping says that the servlet's name is /ExampleWARServlet and the base path is /example, the full path for the example servlet would be /example/ExampleWARServlet.
Listing B.3 shows the web.xml file.
Listing B.3 Source Code for web.xml
<?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"> <display-name>WARExample</display-name> <description> A skeletal application to demonstrate WAR files </description> <servlet> <servlet-name>ExampleWARServlet</servlet-name> <servlet-class>examples.warexample.ExampleWARServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ExampleWARServlet</servlet-name> <url-pattern>/ExampleWARServlet</url-pattern> </servlet-mapping> </web-app>
The root element, <web-app>, defines the boundaries of the deployment descriptor for the Web application. For version 2.4 of the servlet specification, an XML schema is used instead of a DTD in your WAR file.
The Default Deployment Descriptor | There's a web.xml file that provides default values for all Web applications hosted by Tomcat. While you must provide a basic web.xml for your application, you can let Tomcat provide default values for those elements you choose not to define. Tomcat first processes the default descriptor and then any descriptor that you provide with your application. Values that you specify override the default. Tomcat's default web.xml is located in the conf/ subdirectory off of Tomcat's home directory. |
You will soon learn what each element in the web.xml file means. Most are probably obvious because their names are descriptive.
When you create the WAR file, the web.xml file must be in a directory named WEB-INF, and any Java classes you need, like the servlet, must be in a classes directory under WEB-INF. If you have ExampleWARJSP.jsp, ExampleWARServlet.java, and web.xml in the same directory, you can use the following Windows commands to create example.war:
mkdir WEB-INF copy web.xml WEB-INF mkdir WEB-INF\classes javac -d WEB-INF\classes ExampleWARServlet.java jar cvf example.war *.jsp WEB-INF
Required Libraries | Make sure that you have the correct libraries in your classpath before you compile the servlet. For Tomcat, you need to have servlet-api.jar and jsp-api.jar, which are both located in the common/lib subdirectory of Tomcat's installed home. |
If you are running Unix or Linux, the procedure is almost identical:
mkdir WEB-INF cp web.xml WEB-INF mkdir WEB-INF/classes javac -d WEB-INF/classes ExampleWARServlet.java jar cvf example.war *.jsp WEB-INF
|
No comments:
Post a Comment