Creating a .war file

Download Report

Transcript Creating a .war file

Creating a .war file
W(eb) AR(chive)
Creating a .war file
• The following article may contain actual software programs in source
code form. This source code is made available for developers to use
as needed, pursuant to the terms and conditions of this license.
• The Reason
• The new way of deploying Java[tm] 2 Platform, Enterprise Edition
(J2EE[tm]) Web modules that include Java[tm] Servlets and
JavaServer Pages[tm] (JSP[tm]) relies on packaging the application
and associated files into a WebARchive (WAR) file. This seems
simple, as the WAR file is the same format as a JavaARchive (JAR)
file. However, you also need to create an eXtensisible Markup
Language (XML) deployment descriptor (WEB-INF/web.xml) file.
There are tools that can do this for you, such as those in the
Java[tm] Web Services Developer Pack (free download here), or
tools that come with iPlanet[tm] Application Server. However, you
may not want to download or buy these products.
Creating a .war file
• How To
•
•
•
•
•
•
WAR file structure The static HTML files and JSPs are stored in the top level directory.
The Servlet and related Java[tm] technology class files must be stored in
the WEB-INF/classes directory.
Any auxiliary library JAR files must be stored in the WEB-INF/lib directory.
The deployment descriptor is stored as a file named web.xml in the WEBINF directory.
Creating the web.xml deployment descriptor - The following is the
mandatory header of the web.xml document. This defines the document as
an XML file and relates the syntax of the file to the DOCTYPE resource
specified.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
Recall…about web.xml
•
•
Servlets
The simplest form of deployment descriptor that is needed to deploy a Java[tm]
Servlet is as follows (assumes the header is preceding):
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>mypackage.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
The <servlet-name> element is how the Servlet is known within the XML file, the
<servlet-class> is the fully qualified Java programming language class name of the
Servlet; this Java programming language class needs to be located in the WEBINF/classes directory. Thus, for the previous example this would be reflected in the
directory structure: WEB-INF/classes/mypackage/HelloServlet.class.
Creating a .war file
•
The <url-pattern> is how the Servlet is referenced from a Universal
Resource Indicator(URI) - see the "Accessing the Content" section of this
article. According to the Servlet specification it is recommended that you
include a mapping to avoid any potential issues. All of these elements are
documented in the Servlet specifications.
Note: The structure of the elements must follow the order in the DTD
definition. For example all <servlet>s must be defined before any <servletmapping>s can be specified.
•
JSPs
JSPs can be deployed in a WAR file in three ways:
•
Without reference in web.xml
You can put your JSPs directly in the root of the WAR file; the container will
recognise the .jsp extension.
Creating a .war file
• Precompiling
By precompiling the JSP, you have a Servlet. You can reference the
JSP's compiled class (Servlet) and also locate the class as in the
example from the "Servlets" section of this article. However, the <urlpattern> could be something like Foo.jsp, to signify that it is/was a
JSP. For more information, see the JSP Specification note in the
References section of this article.
• Referencing
You can reference the JSP file in web.xml; by using the example in
the "Servlets" section and replacing <servlet-class> with <jspfile>. In this case the <jsp-file> root is the root of the WAR file,
unlike the Servlet scenario.
Creating a .war file
•
Creating the WAR File
After you have created your WEB-INF directory, the deployment descriptor, and have put the
relevant files in the correct directories, you can use the "jar" utility from the Java[tm] Development
Kit distribution to create the WAR file. Check the tools documentation for the full syntax. The
command you could use is (assumes you are at the top level of the directory structure in which
you assembled the WAR contents):
jar cvf mywar.war WEB-INF {related top-level files or directories}
You can then deploy mywar.war using, for example, iPlanet[tm] Application Server, iPlanet[tm]
Web Server or any J2EE compliant application server or Web container.
•
Accessing the Content
The content is generally accessed as follows:
http://host:port/context/{maping or file}
The context could be set in the deployment phase or set automatically by the application. For
example, deploying to iPlanet WebServer 6.x using wdeploy allows you to specify the context
name; but iPlanet Application Server 6.x generally uses the WAR file name as the context.
Making a tea.war file
• The batch file leaves war in current directory, in
this case, my java/bin directory.
• jar cvf tea.war
C:\PROGRA~1\TOMCAT~1.5\WEBAPPS\TEA\
WEB-INF
C:\PROGRA~1\TOMCAT~1.5\WEBAPPS\TEA\T
eaServlet.jar
• This was not a good way to do it because it sets
the name of the servlet jar file in the war file.
You should be able to:
• Drop the war file in the webapps directory
it needs to go in and restart tomcat
• That webapp should now be accessible
To unpack
•
•
•
•
If you need to unzip the war file:
Unpacking the Application Files
A WAR file is basically a Java JAR file, or ZIP file.
Copy the app.WAR file from the installation media to the
directory you would like to install them to.
• Create the sub-directory for the web application.
• Change to the sub-directory.
• Unzip the files using the command
jar -xf ../app.WAR
In fact…
• I never got the webapp working via the
.war file
• I was not able to “unzip” using jar –xf
either.
• These problems may have been due to
the path setting I had when I used jar to
create the war file.
• In any case, winzip can unpack a war file
and it is working fine now.