11.11. Creating WAR Files

Problem

You want to create a deployment file for a web application.

Solution

Compress the web application into a .war file, which is the kind of file you use to deploy web applications. You can create .war files from Eclipse using Ant.

Discussion

To get a look at how to deploy web apps using Eclipse, we’ll deploy the sample servlet in Example 11-8 for a project named DeployApp, which can be found at this book’s O’Reilly site. After compressing an application into a .war file, you can drop the .war file into the Tomcat webapps directory. Tomcat then expands and installs it when you restart Tomcat, which deploys the application.

Creating an application to deploy

To make it easier to create the .war file, give this project its own folder in the webapps directory (i.e., webapps\DeployApp). Don’t forget to add a WEB-INF directory with the subdirectories classes and lib, and make the classes folder the output folder for the project. Then enter the code in Example 11-8 to DeployClass.java.

Example 11-8. A servlet to deploy

package org.cookbook.ch11; import javax.servlet.http.HttpServlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DeployClass extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter( ); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>"); out.println("Deploying ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.