Java Web Applications
03/15/2001
This article is the first in a series of articles on Apache's
Jakarta-Tomcat server. The Tomcat server is a Java-based Web
Application container that was created to run Servlet and JavaServer
Page web applications. It has become the reference implementation for
both the Servlet and JSP specifications. The purpose of this first
article is to give you a basic understanding of web applications. Once
we have this basic understanding, we will be able to move on to
discussions of Tomcat. This article assumes that you have a basic
understanding of Servlets and JSPs.
In this article we will discuss
- the definition of a web application,
- the directory structure of a web application,
- the web application deployment descriptor, and
- packaging a web application.
The definition of a web application
With the release of the Java Servlet Specification 2.2, the concept
of a web application was introduced. According to this specification,
a "Web Application is a collection of servlets, html pages, classes,
and other resources that can be bundled and run on multiple containers
from multiple vendors." To you and me, a web application is anything
that resides in the web layer of an application.
One of the main characteristics of a web application is its
relationship to the ServletContext. Each web application has
one and only one ServletContext. This relationship is controlled by
the servlet container and guarantees that web applications will not
clash when storing objects in the ServletContext.
The following items can exist in a web application:
- Servlets
- JavaServer Pages
- Utility Classes
- Static Documents including, XHTML, images, etc.
- Client side classes
- Meta information that describes the web application
Note: For this series we will be using the proposed Servlet SDK
2.3.
The Directory Structure
The container that holds the components of a web application is the
directory structure in which it exists. The first step in creating a
web application is creating this structure. The following table
contains a sample web application, named onjava, and what
each of its directories should contain. Each one of these directories
should be created from the <SERVER_ROOT> of the
servlet container. An example of a <SERVER_ROOT>,
using Tomcat, would be /jakarta-tomcat-4.0/webapps.
Table 1. The Web Application Directory
Structure |
Directory |
Contains |
/onjava | This is the root
directory of the web application. All JSP and XHTML files are stored
here. |
/onjava/WEB-INF |
This directory
contains all resources related to the application that are not in the
document root of the application. This is where your web application
deployment descriptor is located. Note that the WEB-INF directory is
not part of the public document. No files contained in this directory
can be served directly to a client. |
/onjava/WEB-INF/classes | This directory is
where servlet and utility classes are located. |
/onjava/WEB-INF/lib | This directory
contains Java Archive files that the web application depends upon.
For example, this is where you would place a JAR file that contained a
JDBC driver. |
As you look over the contents of the web application's directory
structure, you will notice that web applications allow for classes to
be stored in both the /WEB-INF/classes and
/WEB-INF/lib directories. Of these two, the class loader
will load classes from the /classes directory first
followed by the JARs in the /lib directory. If you have
duplicate classes in both the /classes and
/lib directories, the classes in the
/classes directory will be used.
The Web application deployment descriptor
At the heart of all web applications is a deployment
descriptor. The deployment descriptor is an XML file named web.xml
located in the
/<SERVER_ROOT>/applicationname/WEB-INF/
directory. It describes configuration information for the entire web
application. For our application the location of the
web.xml file is in the /<SERVER_ROOT>/onjava
/WEB-INF/ directory. The information that is contained in the
deployment descriptor includes the following elements:
- ServletContext Init Parameters
- Localized Content
- Session Configuration
- Servlet / JSP Definitions
- Servlet / JSP Mappings
- Mime Type Mappings
- Welcome File list
- Error Pages
- Security
The following code snippet contains a limited example of a web
application deployment descriptor. As we progress through this
series, we will be looking at the web.xml file and its
elements in much more detail.
<web-app>
<display-name>The OnJava App</display-name>
<session-timeout>30</session-timeout>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.onjava.TestServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
</web-app>
In this example we are setting three application level
elements. The first of the application level elements is the
<display-name>. This element simply describes the
name of the web application. It is functionally inoperative.
The second web application level element is the
<session-timeout> element. This element controls
the lifetime of the application's HttpSession object. The
<session-timeout> value that we have used above
tells the JSP/Servlet container that the HttpSession object will
become invalid after 30 minutes of inactivity.
The last application level element that we have defined is the
<servlet> element. This element defines a servlet
and its properties. We will further define the
<servlet> elements when we discuss deploying
Servlets and JSPs to Tomcat in a subsequent article.
Packaging a Web application
Now that we know what a web application is, we can package it for
deployment. The standard method for packaging web applications is to
use a Web ARchive file (WAR). You can create a WAR file by using
Java's archiving tool jar. An example of this would be to
change to the root directory of your web application and type the
following command:
jar cvf onjava.war .
This command will produce an archive file named
onjava.war that will contain your entire web
application. Now you can deploy your web application by simply
distributing this file, which we will cover in the next article,
"Installing and Configuring Tomcat."
James Goodwill
is the co-Founder of Virtuas Solutions, LLC, a Colorado-based
software consultancy.
Return to ONJava.com.
Comment on this article
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 79 of 79.
-
HTTP Status 404 - /onjava/servlet/com.onjava.login
2007-06-07 01:22:54
DhirajShetty
[Reply | View]
-
HTTP Status 404 - /onjava/servlet/com.onjava.login
2008-02-10 19:13:18
pakornss
[Reply | View]
-
http://localhost:9090/saiapp/HelloWorldExample
2008-03-04 16:43:45
my email
[Reply | View]
-
HTTP Status 404 - /onjava/servlet/com.onjava.login
2007-07-23 12:42:56
RaviKA
[Reply | View]
-
HTTP Status 404 - /onjava/servlet/com.onjava.login
2007-07-23 12:41:20
RaviKA
[Reply | View]
-
HTTP Status 404 - /onjava/servlet/com.onjava.login
2007-09-25 03:00:06
vikaschablani
[Reply | View]
-
HTTP Status 404 - /onjava/servlet/com.onjava.login
2007-09-25 04:14:45
vikaschablani
[Reply | View]
-
HTTP Status 404 - /
2006-08-19 16:32:55
brahmadapa
[Reply | View]
-
HTTP Status 404 - /
2007-01-25 03:47:57
aspai
[Reply | View]
-
HTTP Status 404 - /
2007-03-23 00:06:08
kandiyoorpreethi
[Reply | View]
-
HTTP Status 404 - /
2007-07-23 12:47:54
RaviKA
[Reply | View]
-
HTTP Status 404 - /
2007-07-20 12:36:31
mukkamalas
[Reply | View]
-
HTTP Status 404 - /
2007-03-23 00:06:08
kandiyoorpreethi
[Reply | View]
-
HTTP Status 404 - /
2009-12-06 05:37:57
pinkett
[Reply | View]
-
HTTP Status 404 - /
2007-01-24 02:56:17
vijay-kumar-mishra
[Reply | View]
-
HTTP Status 404 error in The requested resource (/Example) is not available.
2007-01-25 03:45:14
aspai
[Reply | View]
-
HTTP Status 404 error in The requested resource (/Example) is not available.
2007-01-28 21:48:38
aspai
[Reply | View]
-
HTTP Status 404 error in The requested resource (/Example) is not available.
2007-01-28 21:48:29
aspai
[Reply | View]
-
HTTP Status 404 - /
2006-08-25 12:01:14
dmersino
[Reply | View]
-
Status 500?
2006-06-07 06:14:19
ultimastrike
[Reply | View]
-
Status 500?
2007-07-16 07:07:57
AnilaHoney
[Reply | View]
-
Status 500?
2007-01-24 02:52:00
vijay-kumar-mishra
[Reply | View]
-
404 exception for the folder created in webapps
2006-05-05 22:54:52
Sree_nice
[Reply | View]
-
404 exception for the folder created in webapps
2007-03-20 11:55:59
rpjd
[Reply | View]
-
on weblogic8.1
2006-04-27 02:12:15
balu_203
[Reply | View]
-
Http 404 error the resource is not found
2006-01-09 02:03:37
nitinchoube
[Reply | View]
-
place the .class file?
2005-11-23 22:34:24
kethi
[Reply | View]
-
place the .class file?
2006-02-15 21:57:23
subhash10
[Reply | View]
-
differece between web-container
2007-06-20 23:50:19
KumarDeepak
[Reply | View]
-
place the .class file?
2006-07-18 00:33:09
Sujin
[Reply | View]
-
place the .class file?
2006-07-18 00:32:38
Sujin
[Reply | View]
-
place the .class file?
2006-01-09 02:10:02
nitinchoube
[Reply | View]
-
404 error and tomcat server not starting
2005-07-19 19:19:33
wxwx
[Reply | View]
-
HTTP Status 404
2005-02-17 04:46:39
Doro
[Reply | View]
-
Regarding libs in Tomcat Server
2004-10-25 06:19:34
kishans
[Reply | View]
-
type "jar cvf onjava.war" where with what?
2004-04-28 22:00:30
doubleoevan
[Reply | View]
-
type "jar cvf onjava.war" where with what?
2005-01-28 03:32:10
Nataraj-STAG
[Reply | View]
-
type "jar cvf onjava.war" where with what?
2004-09-19 11:51:04
IwaraArikpo
[Reply | View]
-
404 Error
2003-05-15 10:00:34
anonymous2
[Reply | View]
-
404 Error
2005-10-14 18:20:53
rizalina83
[Reply | View]
-
404 Error
2003-10-08 08:48:23
anonymous2
[Reply | View]
-
ultimate...
2003-05-06 02:55:11
anonymous2
[Reply | View]
-
"jsp"
2003-05-05 12:21:13
anonymous2
[Reply | View]
-
Deploying the WAR
2003-03-11 16:07:44
anonymous2
[Reply | View]
-
Deploying the WAR
2005-04-21 01:42:42
kavitha82004
[Reply | View]
-
404 Error - resolution
2003-01-29 08:34:04
anonymous2
[Reply | View]
-
404 Error - resolution
2006-05-05 07:03:16
Sree_nice
[Reply | View]
-
404 Error - resolution
2006-01-09 03:31:16
nitinchoube
[Reply | View]
-
Doesn't work
2003-01-27 03:25:47
anonymous2
[Reply | View]
-
Doesn't work
2005-02-17 02:45:10
Doro
[Reply | View]
-
Doesn't work
2006-01-09 02:59:41
nitinchoube
[Reply | View]
-
thank you
2003-01-13 22:21:17
anonymous2
[Reply | View]
-
thank you
2006-02-22 03:02:23
oo7
[Reply | View]
-
error 404- servlet would not load
2002-12-16 00:19:32
anonymous2
[Reply | View]
-
error 404- servlet would not load
2002-11-18 12:37:46
anonymous2
[Reply | View]
-
error 404- servlet would not load
2003-07-14 04:40:52
anonymous2
[Reply | View]
-
error 404- servlet would not load
2005-11-17 02:41:24
Shreyas
[Reply | View]
-
error 404- servlet would not load
2006-01-09 04:16:57
nitinchoube
[Reply | View]
-
error 404- servlet would not load
2006-04-27 12:24:08
codedmind
[Reply | View]
-
error 404- servlet would not load
2006-01-09 03:55:43
nitinchoube
[Reply | View]
-
error 404- servlet would not load
2007-01-28 20:34:03
arunrash
[Reply | View]
-
Web App series - well worth it
2002-10-13 21:16:38
anonymous2
[Reply | View]
-
Tomcat 4 with Apache [RE]
2002-09-20 01:20:05
anonymous2
[Reply | View]
-
Web Application
2002-05-29 03:24:06
vimalkrishna
[Reply | View]
-
Web Application
2008-06-11 20:08:46
Bharath Kumar
[Reply | View]
-
JavaOne 2002
2002-03-17 17:33:04
dhecksel
[Reply | View]
-
very good and clearly
2002-02-26 19:44:32
yanyu
[Reply | View]
-
PS to the message below
2002-01-16 11:09:52
daveferre
[Reply | View]
-
I, for one, appreciate the hand holding
2002-01-16 11:02:13
daveferre
[Reply | View]
-
hmmm
2001-11-23 00:00:51
tmwhalens
[Reply | View]
-
Too poor
2001-09-19 08:38:42
ni-k
[Reply | View]
-
more more more
2001-06-20 01:51:31
antonrechenauer
[Reply | View]
-
Servlet
2007-10-19 03:17:21
christjesus
[Reply | View]
-
Tomcat 4 with Apache
2001-04-02 21:02:52
purav
[Reply | View]
-
Good Start
2001-04-02 20:58:01
purav
[Reply | View]
-
ahhhhhh MORE!!!
2001-03-28 08:33:41
holjos
[Reply | View]
On running the tutorials on my machine i get the following HTTP error after submitting the login.jsp page . I fail to realise as to where does it contain the pathname folder servlet .
My Web/xml entry is as follows:
<?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>The OnJava App</display-name>
<session-timeout>30</session-timeout>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.onjava.login</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name></param-name>
<param-value></param-value>
</init-param>
</servlet>
</web-app>
Any guidance in this regards will be highly appreciated .
********************ERROR **********************
HTTP Status 404 - /onjava/servlet/com.onjava.login
type Status report
message /onjava/servlet/com.onjava.login
description The requested resource (/onjava/servlet/com.onjava.login) is not available.
Apache Tomcat/5.5.23