Java WAR file description:
Java Web ARchive files are used to bundle web applications in a "zip" file for use with a Java applications server such as Tomcat or JBoss. The Java WAR file is a Java standard for deployment of a bundled group of JSP, Java servlets, html, javascript, images and other files for necessary for the development of web applications.
The WAR file is generated with the Java "jar" command and follows a directory hierarchy as shown here:
WebAppX/ /META-INF/diseinuak4web.net /WEB-INF/diseinuak4web.net | /classes/ | | /diseinuak4web.net | | /diseinuak4web.net | | /diseinuak4web.net | | /aa/bb/cc/diseinuak4web.net | | /com/megacorp/ProjX/ | | | /diseinuak4web.net | | | /diseinuak4web.net | /tags/ /lib/ | /diseinuak4web.net | /diseinuak4web.net | /diseinuak4web.net /diseinuak4web.net /diseinuak4web.net /images/diseinuak4web.net /diseinuak4web.net /diseinuak4web.net /diseinuak4web.net Note:
- /META-INF: This directory and its contents are generated by the "jar" command. Does not have to be explicitly generated.
- /META-INF/services: contents of jar file
- /META-INF/diseinuak4web.net: only one manifest
- /WEB-INF: Required for servlet support and any class files to be called by JSP or servlets. There is only one . It must be defined at the lot directory in the WAR file and can not be nested to a lower directory.
- /WEB-INF/diseinuak4web.net: Required only to register servlets.
- Once deployed, this web application would be called using the URL
- Servlet URLs: is mapped from its class name to its URL name by the diseinuak4web.net file.
ServletC mapping to URL:
Command:
Where this command is executed in the same directory which contains the diseinuak4web.net (or diseinuak4web.net) and WEB-INF directory.
Also see the jar man page
WAR file and Servlets:
Where the file maps the servlet name to the URL display name.
<?xml version="" encoding="ISO"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application //EN" "diseinuak4web.net"> <web-app> <display-name>Welcome to our server</display-name> <description>Welcome to our server</description> <servlet> <servlet-name>Servlet-A</servlet-name> <servlet-class>ServletA</servlet-class> </servlet> <servlet> <servlet-name>Servlet-B</servlet-name> <servlet-class>ServletB</servlet-class> </servlet> <servlet> <servlet-name>Servlet-C</servlet-name> <servlet-class>diseinuak4web.nettC</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet-A</servlet-name> <url-pattern>/ServletA</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Servlet-B</servlet-name> <url-pattern>/ServletB</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Servlet-C</servlet-name> <url-pattern>/ServletC</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>diseinuak4web.net</welcome-file> <welcome-file>diseinuak4web.net</welcome-file> <welcome-file>diseinuak4web.net</welcome-file> </welcome-file-list> </web-app>
Note that this example shows a file defining three separate servlets to be deployed in the WAR file. Where XML tags must appear in the following order:
- <context-param>
- <filter>
- <filter-mapping>
- <listener>
- <servlet>
- <servlet-mapping>
- <session-config>
- <mime-mapping>
- <welcome-file-list>
- <error-page>
- <taglib>
- <resource-env-ref>
- <resource-ref>
- <security-constraint>
- <login-config>
- <security-role>
- <env-entry>
- <ejb-ref>
- <ejb-local-ref>
Note that the following will
NOT work:
<servlet> .. </servlet> <servlet-mapping> .. </servlet-mapping> <servlet> .. </servlet> <servlet-mapping> .. </servlet-mapping>
Must order all <servlet> tags together then <servlet-mapping>, etc When not ordered properly you will get the error: Caused by: diseinuak4web.neteption: The content of element type "web-app" must match
Example servlet:
import diseinuak4web.net*; import diseinuak4web.net*; import diseinuak4web.net*; import diseinuak4web.net*; import diseinuak4web.nett.*; import diseinuak4web.net*; public class ServletA extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doit(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doit(request, response); } /** * Class description goes here * * @param request * @param response * @throws ServletException * @throws IOException */ protected void doit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String value = diseinuak4web.netameter("param-name"); } }
JSP call to class in WAR file:
<%@ page language="java" import="diseinuak4web.net" %> <% Abc abc = new Abc(); String sss = diseinuak4web.netA(argx); %>
[Potential Pitfall]: incorrect object instantiation
Error: Syntax error on token "new", delete this token Fix:
Ant file to generate a WAR file:
file:
<?xml version="" encoding="UTF-8"?> <project name="ProjX" default="all" basedir="."> <description>Builds, tests, and runs the project ProjX.</description> <property name="diseinuak4web.net" value="./"/> <property name="diseinuak4web.net" location="${diseinuak4web.net}/src/lib"/> <property name="diseinuak4web.net" location="${diseinuak4web.net}/src/servlets"/> <!-- Classpath to find servlet and java packages --> <path id="diseinuak4web.net"> <pathelement location="/usr/java/latest/lib/diseinuak4web.net" /> <pathelement location="/opt/jbossM4/lib/diseinuak4web.net" /> <pathelement location="/opt/jbossM4/client/jboss-servlet-api__diseinuak4web.net" /> </path> <target name="usage"> <echo message="Available targets are:"/> <echo message="clean - Remove diseinuak4web.net and class files."/> <echo message="war - Generate war file to deploy to JBoss."/> <echo message="war-deploy - Copy war file to JBoss war file deployment directory."/> </target> <target name="clean" description="Remove .class and .jar files"> <delete includeEmptyDirs="true" failonerror="false"> <fileset dir="${diseinuak4web.net}"> <include name="**/*.class"/> </fileset> <fileset dir="${diseinuak4web.net}"> <include name="**/*.class"/> </fileset> <fileset dir="WEB-INF/classes"> <include name="**/*.class"/> </fileset> <fileset dir="./"> <include name="diseinuak4web.net"/> </fileset> </target> <target name="compile-war-lib"> <mkdir dir="WEB-INF/classes"/> <javac srcdir="${diseinuak4web.net}" destdir="WEB-INF/classes" debug="true" includeAntRuntime="false"> <classpath refid="diseinuak4web.net"/> <include name="**/*.java"/> </javac> </target> <target name="compile-war" depends="compile-war-lib"> <mkdir dir="WEB-INF/classes"/> <javac srcdir="${diseinuak4web.net}" destdir="WEB-INF/classes" debug="true" includeAntRuntime="false"> <classpath refid="diseinuak4web.net"/> <classpath refid="diseinuak4web.net" /> <include name="**/*.java"/> </javac> </target> <target name="war" depends="compile-war,other-dependencies-go-here"> <war destfile="diseinuak4web.net" webxml="WEB-INF/diseinuak4web.net"> <fileset dir="./"> <include name="**/*.jsp"/> <include name="**/*.css"/> <include name="**/*.png"/> <include name="diseinuak4web.net"/> <include name="WEB-INF/**/*.class"/> <exclude name="**/.svn/**"/> <exclude name="test/**"/> <exclude name="src/**"/> </fileset> </war> </target> <target name="war-deploy" depends="war" description="Deploy application as a WAR file"> <copy todir="${diseinuak4web.net}" preservelastmodified="true"> <fileset dir="."> <include name="*.war"/> </fileset> </copy> </target> <target name="all" depends="war" /> </project>
Note the use of the <war> ant task. The <jar> ant task can also be used to generate a WAR file but jar is more work as the user must specify much more detailed information. Given the following source tree: ProjX/src/servlets/diseinuak4web.net | | /diseinuak4web.net | /lib/com/megacorp/YYY/XXX/diseinuak4web.net | | /diseinuak4web.net /deploy/WebAppX/ /diseinuak4web.net /diseinuak4web.net /WEB-INF/diseinuak4web.net | /classes/ | | /diseinuak4web.net | | /diseinuak4web.net | | /com/megacorp/YYY/XXX/diseinuak4web.net | | | /diseinuak4web.net / or some put the source in deployed folders and let ant pick what is needed to assemble the WAR file: ProjX/WebAppX/diseinuak4web.net /diseinuak4web.net /WEB-INF/diseinuak4web.net | /classes/ | | /diseinuak4web.net | | /diseinuak4web.net | | /src/ | | /src/servlets/diseinuak4web.net | | /src/servlets/diseinuak4web.net | | /src/com/megacorp/YYY/XXX/diseinuak4web.net | | | /diseinuak4web.net | | /com/megacorp/YYY/XXX/diseinuak4web.net | | | /diseinuak4web.net
Tomcat:
Copy the WAR file to
Where that resolves to (RHEL6 RPM package):
Tomcat 8 Apache binary download installation:
JBoss:
Simply copy the WAR file to
Exploded WAR files: Note that the WAR file can be "un-jar'ed" and expanded on the file system.
$JBOSS_HOME/server/default/deploy/diseinuak4web.net /classes/ Sometimes this eases JSP development but can cause servlet complications as well. The JBoss server will not recognize servlet updates and you need to restart the JBoss server in order to pick up the changes. JSP development is simplified as JSP pages can be edited in place and tried immediately.
Use the URL
Individual JSP pages can also be developed in the default WAR directory:
Use the URL
Hello World Tomcat servlet example WAR file:
Directory set-up:
File:
package diseinuak4web.nettx; import diseinuak4web.net*; import diseinuak4web.net*; import diseinuak4web.net*; import diseinuak4web.nett.*; import diseinuak4web.net*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { diseinuak4web.nettentType("text/html"); PrintWriter out = diseinuak4web.netter(); diseinuak4web.netn("<html>"); diseinuak4web.netn("<head>"); diseinuak4web.netn("<title>Hello World!</title>"); diseinuak4web.netn("</head>"); diseinuak4web.netn("<body>"); diseinuak4web.netn("<h1>Hello World!</h1>"); diseinuak4web.netn("</body>"); diseinuak4web.netn("</html>"); } }
File:
<?xml version="" encoding="UTF-8"?> <web-app xmlns="diseinuak4web.net" xmlns:xsi="diseinuak4web.net" xsi:schemaLocation="diseinuak4web.net diseinuak4web.net" version="" metadata-complete="true"> <description>Hello World</description> <display-name>Hello World</display-name> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>diseinuak4web.netorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> </web-app>
This associates the class to a URL path Maven Build File:
<project xmlns="diseinuak4web.net" xmlns:xsi="diseinuak4web.net" xsi:schemaLocation="diseinuak4web.net diseinuak4web.net"> <modelVersion></modelVersion> <groupId>diseinuak4web.nettx</groupId> <artifactId>ProjectX</artifactId> <packaging>war</packaging> <version>SNAPSHOT</version> <name>projectx</name> <url>diseinuak4web.net</url> <properties> <diseinuak4web.netEncoding>UTF-8</diseinuak4web.netEncoding> </properties> <dependencies> <dependency> <groupId>diseinuak4web.nett</groupId> <artifactId>diseinuak4web.nett-api</artifactId> <version></version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>diseinuak4web.nets</groupId> <artifactId>maven-war-plugin</artifactId> <version></version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <webappDirectory>/opt/apache-tomcat/webapps/projectx/</webappDirectory> </configuration> </plugin> <plugin> <groupId>diseinuak4web.nets</groupId> <artifactId>maven-compile-plugin</artifactId> <version></version> <configuration> <source></source> <target></target> </configuration> </plugin> </plugins> </build> </project>
[Potential Pitfall]: Error:
POM file requires dependency "diseinuak4web.nett-api" to fix this error. Build:
- Generate WAR file:
This will generate - Deploy WAR file:
This will install the servlet to Tomcat's "webappDirectory"
Inspect the contents of the war file: META-INF/diseinuak4web.net META-INF/ WEB-INF/ WEB-INF/classes/ WEB-INF/classes/com/ WEB-INF/classes/com/megacorp/ WEB-INF/classes/com/megacorp/projectx/ WEB-INF/classes/com/megacorp/projectx/diseinuak4web.net WEB-INF/diseinuak4web.net META-INF/maven/diseinuak4web.nettx/Hello/diseinuak4web.net META-INF/maven/diseinuak4web.nettx/Hello/diseinuak4web.netties Test: http://localhost/projectx/HelloWorld

Books:
 | "Core Java 2, Volume 1: Fundamentals " by Cay S. Horstmann, Gary Cornell ISBN # , Prentice Hall PTR 8th edition The industry standard. Need I say more? | 
|
 | "Core Java 2: Volume 2 Advanced Features " by Cay S. Horstmann, Gary Cornell ISBN # , Prentice Hall PTR 8th edition The industry standard. Need I say more? | 
|
 | "Core Java Server Faces" by David Geary, Cay S. Horstmann ISBN # , Prentice Hall PTR 2nd edition | 
|
 | "JSP, Servlets, and MySQL" by David Harms ISBN # , Hungry Minds, Inc | 
|
-
-
-