Chapter 11 Deploying your web app

Download Report

Transcript Chapter 11 Deploying your web app

CHAPTER 11 DEPLOYING YOUR
WEB APP
WAR FILE
•
•
•
A WAR file is simply a snapshot of your web app
structure, in a nice portable, compressed form (it
is really just a JAR file).
You jar up your entire web app structure and
give it a .war extension.
Thus instead of copy static web pages, JSP pages,
servlets, and web.xml separately to appropriate
locations of Tomcat for deployment, we can
simply copy this single WAR file to tomcat’s
webapps directory.
WAR FILE
•
•
Tomcat will be responsible for unpack the
WAR file and create a corresponding
application context/directory and put all
extracted files there.
In Tomcat, the name of the WAR file
becomes the web app name.
–
That is, if you drop the file lab8.war to
Tomcat’s webapps directory, a new directory
called lab8 will be created under the webapps
directory.
WAR FILE


If you re-deploy the application by dropping an updated
WAR file to tomcat’s webapps directory, tomcat will first
remove the application context/directory, then extract files
from the updated WAR file, and re-create the application
context/dir for the application.
 Another advantage: You do not have to restart your
Tomcat server after the re-deployement even if you have
servlets inside your application
If you want to un-deploy the application, simply delete the
WAR file from tomcat’s webapps directory (You do not need
to remove the unpacked directory, tomcat will be able to
automatically remove it)
What a deployed WAR
file looks like
Note: META-INF and MANIFEST.MF under it are
automatically generated and MANIFEST.MF is
used for specifying optional libraries, e.g., the
libraries the container cannot find automatically.
• Classes you deploy in the WEBINF/classes and JAR files in WEB-INF/lib are
available to the container and you don’t have
to say anything
MAKING STATIC CONTENT AND JSPS
DIRECTLY ACCESSIBLE
•
•
When you deploy static HTML and JSPs, you can
choose whether to make them directly accessible
from outside the web app.
By directly accessible, we mean that a client can
enter the path to the resource into his browser,
and the server will return the resource.
–
You can prevent direct access by putting files under
WEB-INF
USE ANT TO BUILD THE WAR FILE

You can use Ant task war to build a WAR file

Two options:
Use ANT war task
 Use ANT jar task

(only part of the build file is shown here)
<?xml version="1.0" ?>
<project name="myproject" default="deploy">
<!-- ==== create directories for development environment and deployment environment ==== -->
<target name="init">
<mkdir dir="etc" />
<mkdir dir="classes" />
<mkdir dir="src" />
<mkdir dir="web" />
<!-- This dir will contain the WAR file -->
<mkdir dir="dist"/>
</target>
<!-- ==== compile the java source ==== -->
<target name="compile" depends="init">
<javac srcdir="src"
destdir="classes">
<classpath refid="compile.classpath"/>
</javac>
</target>
<!-- ==== build the war file ==== -->
<target name="dist" depends="compile">
<war destfile="dist/${application}.war" webxml="etc/web.xml">
<fileset dir="web"/>
<classes dir="classes"/>
</war>
</target>
WAR FILE
USE ANT TO BUILD WAR FILE

If you have third party library JAR files, when
deployed, it is located at WEB-INF/lib direcotry
of your web application.

If you build a WAR file, you may include these
libraries in corresponding place
<!-- ==== create directories for development environment and
deployment environment ==== -->
<target name="init">
<mkdir dir="etc" />
<mkdir dir="classes" />
<mkdir dir="src" />
<mkdir dir="web" />
<mkdir dir="lib" />
You may create a dir lib to
store other third party library
such as JDBC driver
<!-- This dir will contain the WAR file -->
<mkdir dir="dist" />
</target>
<!-- ==== compile the java source ==== -->
<target name="compile" depends="init">
<javac srcdir="src" destdir="classes">
<classpath refid="compile.classpath" />
</javac>
</target>
<!-- ==== build the war file ==== -->
<target name="dist" depends="compile">
<war destfile="dist/${application}.war"
webxml="etc/web.xml">
<fileset dir="web" />
<lib dir="lib" />
<classes dir="classes" />
</war>
</target>
Pack the third party library
here
WAR FILE

We can use ANT jar task to build the WAR file
too.
In this case, you create another dir called build as
the place for assembling all the files in this place.
 Using ANT war task is more convenient, since this
assembly step is avoided.

<target name="init">
………………….
<!-- This dir is the place where we assembly different parts of WAR file -->
<mkdir dir="build" />
<mkdir dir="build/WEB-INF" />
<mkdir dir="build/WEB-INF/classes" />
<mkdir dir="build/WEB-INF/lib" />
<!-- This dir will contain the WAR file -->
<mkdir dir="dist" />
</target>
<!-- ==== build the war file ==== -->
<target name="dist" depends="compile">
<copy todir="build/WEB-INF/lib">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</copy>
<copy todir="build/WEB-INF/classes">
<fileset dir="classes">
<include name="**/*.*" />
</fileset>
</copy>
<copy file="etc/web.xml" todir="build/WEB-INF" />
<copy todir="build">
<fileset dir="web">
<include name="**/*.*" />
</fileset>
</copy>
<jar destfile="dist/${application}.war" basedir="build"/>
</target>
Directory build and its subdirectories are places for
assembling files
<!-- ==== build the war file ==== -->
<target name="dist" depends="compile">
<copy todir="build/WEB-INF/lib">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</copy>
<copy todir="build/WEB-INF/classes">
<fileset dir="classes">
<include name="**/*.*" />
</fileset>
</copy>
<copy file="etc/web.xml" todir="build/WEB-INF" />
<copy todir="build">
<fileset dir="web">
<include name="**/*.*" />
</fileset>
</copy>
<jar destfile="dist/${application}.war"
basedir="build"/>
</target>
Since WAR file is simply
a JAR file, we can use
ANT jar task to jar the
files we assembled in
the build directory.
HOW TO FIND OUT WHAT IS INSIDE THE
GENERATED WAR FILE
• If you want to find out what is
inside the generated WAR file,
you can use the command:
C:\J2EE_C~1\LAB9_A~2\LAB9_A~1\dist>jar tvf lab9a_2.war
0 Tue Nov 23 13:53:28 EST 2010 META-INF/
106 Tue Nov 23 13:53:26 EST 2010 META-INF/MANIFEST.MF
0 Tue Nov 23 13:53:28 EST 2010 WEB-INF/
jar tvf name_of_WAR_file
0 Tue Nov 23 13:53:28 EST 2010 WEB-INF/classes/
0 Tue Nov 23 13:53:28 EST 2010 WEB-INF/classes/edu/
• You may noticed that when you
use ant war task or jar task to
generate the WAR file, it
automatically creates a directory
called META-INF and a file
MANIFEST.MF under it.
0 Tue Nov 23 13:53:28 EST 2010 WEB-INF/classes/edu/nku/
0 Tue Nov 23 13:53:28 EST 2010 WEBINF/classes/edu/nku/j2ee/
0 Tue Nov 23 13:53:28 EST 2010 WEB-INF/lib/
235 Tue Nov 23 13:53:28 EST 2010 WEBINF/classes/edu/nku/j2ee/CounterBean.class
304 Tue Nov 23 13:53:28 EST 2010 WEB-INF/web.xml
481 Tue Nov 23 13:53:28 EST 2010 counter.jsp
488 Tue Nov 23 13:53:28 EST 2010 index.htm
2573 Tue Nov 23 13:53:28 EST 2010 index.jpg
616 Tue Nov 23 13:53:28 EST 2010 prog.jsp
DEPLOY WAR FILE
<!-- ==== deploy the files to Tomcat ==== -->
<target name="deploy" depends="dist">
<copy file="dist/${application}.war"
todir="${tomcat.home}/webapps" />
</target>

This simply drop the file under webapps dir of
tomcat.