9.7 Deploying Web Applications

     

The last topic we'll take a look at in this chapter is all about deploying web applications. This process is easy enoughall you've got to do is create a compressed Web Archive (WAR) file of your application and drop it into the Tomcat webapps directory. The next time Tomcat is restarted, it'll expand that WAR file automatically, deploying the application.

As an example, we're going to deploy the servlet you see in Example 9-11. To make the WAR file creation easier, create this new project, Ch09_07 , in its own folder, Ch09_07 , in the webapps directory. Make this project a standard Java project (not a Tomcat project). Be sure you give the Ch09_07 folder its own WEB-INF directory with the subdirectories classes and lib , and make the classes folder the output folder for the project. Now create Ch09_07.java as we have before, and, after entering the servlet's code into Ch09_07.java , build the project.

Example 9-11. A servlet to deploy
 package org.eclipsebook.ch09; import javax.servlet.http.HttpServlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Ch09_07 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("A Web Page");                 out.println("</TITLE>");                 out.println("</HEAD>");                 out.println("<BODY>");                 out.println("<H1>");                 out.println("Project Deployment!");                 out.println("</H1>");                 out.println("</BODY>");                 out.println("</HTML>");         } } 

Also add the web.xml file you see in Example 9-12 to the project's WEB-INF folder to let Tomcat know about this servlet. That gets our servlet working, as you can check by navigating to http://localhost:8080/Ch09_07/org.eclipsebook.ch09.Ch09_07 .

Example 9-12. web.xml for the servlet 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>Ch09_07</servlet-name>     <servlet-class>org.eclipsebook.ch09.Ch09_07</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>Ch09_07</servlet-name>     <url-pattern>/org.eclipsebook.ch09.Ch09_07</url-pattern>   </servlet-mapping> </web-app> 

The next step is to deploy this servlet by compressing it into a WAR file. An easy way to do that in Eclipse is to use Ant, and that can be done with the build.xml file you see in Example 9-13, which will compress all the files in the project into a WAR file named Ch09_07war.war .

Example 9-13. build.xml for the servlet to deploy
 <?xml version="1.0" encoding = "UTF-8"?> <project name="Ch09_07" default="Main Build" basedir=".">         <property name="bin"          location="d:/tomcat/jakarta-tomcat-4.1.29/webapps/ch09_07"/>         <property name="wardir"          location="d:/tomcat/jakarta-tomcat-4.1.29/webapps/ch09_07"/>         <property name="warfile" location="${wardir}/Ch09_07war.war"/>          <property name="build.compiler"          value="org.eclipse.jdt.core.JDTCompilerAdapter"/>                   <target name="Main Build" depends="War">                 <echo message="Ant at work!"/>         </target>                  <target name="War" >                 <jar destfile="${warfile}" basedir="${bin}"/>         </target> </project> 

To create the WAR file, add build.xml to the project. Then right-click build.xml , select Run Ant, and click the Run button to create the WAR file Ch09_07war.war .

You can deploy the entire web application with this one filejust drop it into the Tomcat webapps directory on the user 's machine and restart Tomcat. Taking its cue from the name of the WAR file, Tomcat will install the web application in a directory named Ch09_07war (note that we didn't name this WAR file Ch09_07.war because when Tomcat expanded that WAR file it would have overwritten the existing Ch09_07 directory).

When the application has been deployed and installed by Tomcat this way, you can run it by navigating to http://localhost:8080/Ch09_07war/org.eclipsebook.ch09.Ch09_07 in a browser, as you see in Figure 9-17. The servlet has been completely deployed.

Figure 9-17. Deploying a project
figs/ecps_0917.gif

That completes this chapter on Eclipse and web developmentas you can see, using Eclipse for web development is natural. Creating JSPs, creating servlets, installing files, starting and stopping Tomcat, creating web deployment packagesall of these tasks are no problem with Eclipse.



Eclipse
Eclipse
ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

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