Recipe 11.11 Creating WAR Files

     

11.11.1 Problem

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

11.11.2 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.

11.11.3 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.

11.11.3.1 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 a Project");         out.println("</TITLE>");         out.println("</HEAD>");         out.println("<BODY>");         out.println("<H1>");         out.println("Deploying a Project");         out.println("</H1>");         out.println("This deployed project is functional.");         out.println("</BODY>");         out.println("</HTML>");     } } 

The web.xml file we'll use for this project appears in Example 11-9; place it in the project's WEB-INF folder. Then build the project, (re)start Tomcat, and navigate to http://localhost:8080/DeployApp/org.cookbook.ch11.DeployClass to confirm that the servlet is working. This is the servlet that we're going to deploy.

Example 11-9. web.xml for the web application to deploy
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http:// java.sun.com/dtd/web-app_2_3.dtd"> <web-app>   <display-name>Example Applications</display-name>   <servlet>     <servlet-name>DeployApp</servlet-name>     <servlet-class>org.cookbook.ch11.DeployClass</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>DeployApp</servlet-name>     <url-pattern>/org.cookbook.ch11.DeployClass</url-pattern>   </servlet-mapping> </web-app> 

11.11.3.2 Creating a .war file to deploy

We've built the servlet we want to deploy, and the next step is to package it into a deployable .war file. The best way to do that in Eclipse is to use Ant, and we'll use the build.xml file in Example 11-10. This build.xml file takes the output of the DeployApp project and compresses it into a .war file named DeployAppWar.war , storing it in webapps\DeployApp .

Example 11-10. build.xml for the servlet to deploy
 <?xml version="1.0" encoding="UTF-8" ?>  <project name="DeployApp" default="Main Build" basedir=".">   <property name="project" location="d:/tomcat/jakarta-tomcat-5.0.19/webapps/DeployApp" />    <property name="wardir" location="d:/tomcat/jakarta-tomcat-5.0.19/webapps/DeployApp" />    <property name="warfile" location="${wardir}/DeployAppWar.war" />      <target name="Initialize">         <delete file="${warfile}" />     </target>     <target name="War">         <jar destfile="${warfile}" basedir="${project}" />      </target>     <target name="Main Build" depends="Initialize, War">         <echo message="Building the .war file" />      </target> </project> 

Add build.xml to the project. After building the project, right-click build.xml , select Run Ant, and click Run to create DeployAppWar.war .

11.11.3.3 Deploying a .war file

After creating DeployAppWar.war , you can deploy the application just by placing that file in the Tomcat webapps directory on the target machine and (re)starting Tomcat. Tomcat uses the name of the .war file, expands that file, and installs the web application in a directory named DeployAppWar .

After dropping DeployAppWar.war into the webapps directory, (re)start Tomcat, and navigate to http://localhost:8080/DeployAppWar/org.cookbook.ch11.DeployClass to check this out. You should see the results shown in Figure 11-15. Now you're using Eclipse to create deployment packages for web applications. Not bad.

Figure 11-15. A deployed project
figs/ecb_1115.gif

You also can use Ant to deploy the .war file directly, using such Ant tasks as ftp to send the .war file to the web server directory you want on remote machines.


11.11.4 See Also

Recipe 7.1 in Chapter 7 on connecting Ant to Eclipse; Chapter 3 in Tomcat: the Definitive Guide (O'Reilly); Chapter 2 in the Java Servlet and JSP Cookbook (O'Reilly).



Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net