Вы находитесь на странице: 1из 9

STRUTS FRAMEWORK - An Overview of Configuring WEB.

XML

Author(s): Puneet Chopra/Prashant Kankipati

Date written (MM/DD/YY): 03/10/04

Declaration:

I/We hereby declare that this document is based on my/our personal experiences
and / or experiences of my/our project members. To the best of my/our knowledge,
this document does not contain any material that infringes the copyrights of any
other individual or organization including the customers of Infosys.

Puneet Chopra/Prashant Kankipati

Project Details

• Projects involved: Harrahs Casino - RG2


• H/W Platform: Dell PC
• S/W Environment: JAVA, Struts Framework, Tomcat 4.1
• Application. Type: Web application
• Project Type : Development

Target readers: Developers, Project Leaders

Keywords

Web.xml, Struts Framework, Configuration information, Deployment Descriptor,


DTD, Served, Elements of Web.xml

1
Table of Content
AUTHOR(S): PUNEET CHOPRA/PRASHANT KANKIPATI............................................................................................1
DATE WRITTEN (MM/DD/YY): 03/10/04........................................................................................................1
DECLARATION:.................................................................................................................................................1
PUNEET CHOPRA/PRASHANT KANKIPATI..............................................................................................................1
PROJECT DETAILS ............................................................................................................................................1
TARGET READERS: DEVELOPERS, PROJECT LEADERS..............................................................................................1
KEYWORDS ....................................................................................................................................................1
WEB.XML, STRUTS FRAMEWORK, CONFIGURATION INFORMATION, DEPLOYMENT DESCRIPTOR, DTD, SERVED, ELEMENTS
OFWEB.XML....................................................................................................................................................1
1 OVERVIEW.................................................................................................................................................3
1.1 INTRODUCTION............................................................................................................................................3
1.2 SCOPE OF THE DOCUMENT.............................................................................................................................3
1.3 TARGET AUDIENCE......................................................................................................................................3
2 INTRODUCTION TO WEB.XML............................................................................................................3
3 OVERVIEW OF DEPLOYMENT DESCRIPTOR ........................................................................4
3.1 DEPLOYMENT DESCRIPTOR- DOCUMENT TYPE DEFINITION................................................................4
3.2 CONSTRAINTS IN DTD................................................................................................................................4
4 SAMPLE WEB.XML.................................................................................................................................5
4.1 ELEMENTS OF WEB.XML..............................................................................................................................6
5 CONCLUSION............................................................................................................................................9
6 REFERENCES............................................................................................................................................9

2
1 Overview

1.1 Introduction

This document will give you the basic knowledge about the role WEB.XML plays in
configuration information in developing a web application in using struts framework
and what all are its elements and sub elements.

1.2 Scope of the document

This document covers the the overview of web.xml used for configuration information in
struts framework and a brief explanation of the Document Type Definition (DTD)
follwed for creation of xml documents.

1.3 Target Audience

The target audience for this document are developers working on web projects using
struts framework. The readers are expected to have little hands on experience on
following aspects:

- Understanding of J2EE architecture.


- Basic knowledge of struts framework.

2 Introduction to Web.xml

Whenever we are developing a web application using Servlet 2.2 compliant servers such
as Tomcat, we use a specific folder structure, keeping our files in the webapps folder
placed in our application folder. Thus if we make any changes in the application we just
save those changes and restart the server, but to change the configuration information of a
particular application we need to modify a special file called web.xml placed in the web-
inf directory which also contains some other important directories such as struts-
confg.xml containing description of all the struts components, the struts-bean.tld used for
tag libraries etc.

Now if the web application we are developing mainly consists of simple HTML and JSP
then web.xml doesn’t come into picture, but if in case we are using the tag libraries
(Fig-1.1) etc.

3
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

Fig-1.1

then we need to define the configuration information and here web.xml comes into
picture.

We’ll go through a sample web.xml later but for the picture to be more clear we need to
see one simple aspect regarding creation of xml documents which you’ll find interesting
and of course worthy.

3 Overview of Deployment Descriptor

3.1 DEPLOYMENT DESCRIPTOR- Document Type Definition

Now every XML document we make has to follow certain rules and these constraints are
well defined in the DTD (Document Type Definition). The DTD defines a set of
constraints for creating an xml document, the elements an xml can contain, what should
be the order of those elements, mandatory and optional parameters etc. Some of the
constraints mentioned in a DTD are summarized as:-

3.2 Constraints in DTD

 The elements in an xml document have to be in the order specified in the DTD
sample above, which means element icon to start with, followed by element display-
name, followed by element description and so on.

 A question mark (?) after any element in the DTD indicates that it’s optional.

 An asterisk (*) after any element indicates that it can be used zero or more times.

4
 A plus sign (+) after any element indicates that it can be used one or more times.

Likewise there are other constraints defined in the standard XML DTD which we need
not study in detail as of now. But by going through this sample we are at least clear that
we need to follow certain rules while creating an xml document.

Now we’ll take a walk through a sample web.xml (Fig-1.2). which I used in a sample
application.

4 Sample WEB.XML

Fig-1.2

<?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">

<web-app>

<!-- Standard Action Servlet Configuration (with debugging) -->


<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Standard 0 -->


<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- The Usual Welcome File List -->


<welcome-file-list>
<welcome-file>Profile.jsp</welcome-file>
</welcome-file-list>

5
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>

<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>

4.1 Elements of Web.xml

 The very first line <?xml version="1.0" encoding="ISO-8859-1"?>


is the declaration part and indicates the version of the xml.

 The next part – Fig1.3

6
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

Fig1.3

refers to the DTD explained above and helps identify the DTD for the current web.xml
which will have all the required definitions of the xml’s elements, their order and all.

 The <web-app> is the very first element of the DTD and indicates all the mandatory
and optional elements it can contain and their order.

 Next comes the servlet configuration element <servlet> (Fig1.4) which is used to
describe the name and the class and its initial parameters. In ActionServlet Config we
can define the same servlet class with different names, passing different initial
parameters to them. In that case different instances of that servlet class will be created
and referred.

<!-- Standard Action Servlet Configuration -->


<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Fig-1.4

Besides Config as initial parameter as we see in the sample above there are some
optional parameters such as Debug whose value indicates the amount of information
to be logged from the server and this value is 0 by default.

• Now we see the element <servlet> contains another sub element <load-on-
startup>1</load-on-startup> in Fig1.4 and as the name suggests this sub element
tells the server to load a particular servlet when the application starts. The integral

7
value in this sub element which is one here indicates when to load that particular
servlet, a lower valued servlet will be loaded before a higher valued one.

 Now we see the <servlet-mapping> element which maps the


request to a particular servlet. (Fig1.5). There are different types of mappings which
we can specify in the <url-pattern>

<!-- Standard 0 -->


<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Fig1.5

In this case the *.do specified in the url pattern is an Extension mapping which says that
the action servlet will be fulfilling all the requests terminating with .do extension. As seen
above in the servlet configuration this action servlet is mapped to the class
org.apache.struts.action.ActionServlet.

• The <welcome-file-list> element (Fig1.6) in simple words opens the jsp


page mentioned in the welcome file when the application is started, profile.jsp in
this case.

<!-- The Usual Welcome File List -->


<welcome-file-list>
<welcome-file>Profile.jsp</welcome-file>
</welcome-file-list>

Fig1.6

It appends that file after the usual http://localhost8080/struts-app/profile.jsp

• Last but not the least in the sample above comes the <taglib> element
(Fig1.7) which maps all the .tld’s (Tag Library Descriptors) specified inside the
<taglib-uri> to their appropriate location specified in the <taglib- location>which
usually is in the WEB-INF folder of our directory structure. Here we can see all
the tld’s such as the struts-logic, struts-bean, struts-html etc.

Fig1.7

<!-- Struts Tag Library Descriptors -->


<taglib>

8
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>

<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

5 CONCLUSION

Conclusion: After going through this document one is now clear about the role web.xml
plays in a web application using struts framework and what all are the elements and sub
elements in this file and how these elements and sub elements behave.

6 References

S.No. Book referred/web site Author


1 http://jakarta.apache.org/struts
2 Java Server Pages Hans Bergston