The Deployment Descriptor: web.xml
Before I explain what a deployment descriptor is, we should first understand the web application directory structure
WEB APPLICATION DIRECTORY STRUCTURE:
A
Web application contains a structure hierarchy of directories and files in a
standard layout. Such a hierarchy can be accessed in its "unpacked"
form, where each directory and file exists in the filesystem separately(This format is more useful during development), or in
its "packed" WAR file(This format is used when you distribute your application to be installed). Any Web Application that is to be deployed in Tomcat has to
conform to this directory structure. The format of the structure can be seen in
the figure below:
Web Application Directory Structure |
- Application Root
Directory - This is the root directory
of the Web Application. The name of this directory is named by the
developer - for example the default context we created for this module was
called "student". All JSP, HTML and image files are stored here.
Typically, there would be an index.html or index.jsp file in this
directory. The "other directories" could be, for example,
"images" (containing gifs and jpgs for the html pages and
servlets used in the Application), "style" (containing the .css
or other style files used) or simply subdirectories of HTML or JSP pages.
- /WEB-INF -
This directory contains all resources related to the application that are
not in the document root of the application. It should be noted that the
WEB-INF directory is not part of the public document (ie. paths beginning
in http://localhost:8080/student/WEB-INF cannot be viewed in a browser)
and no files contained in this directory can be served directly to the
client. The WEB-INF directory also contains the Web Application
Deployment Descriptor.
- /WEB-INF/classes -
contains the application Java Servlets, class files, JavaBeans and utility
classes.
- /WEB-INF/lib -
The lib directory contains the Java ARchive (JAR) files (or ZIP) that the
web application depends upon. For example, this directory might contain a
JDBC Database Connectivity driver in JAR or Zip format.
- /WEB-INF/web.xml - The deployment descriptor is an XML file named web.xml and forms the heart of the Web Application. This file is discussed below.
Web Application Deployment Descriptor:
A web application's
deployment descriptor describes the classes, resources and configuration of the
application and how the web server uses them to serve web requests. When the
web server receives a request for the application, it uses the deployment
descriptor to map the URL of the request to the code that ought to handle the
request. This file is named web.xml, and
resides in the app's WAR under the WEB-INF/ directory. web.xml is part of the
servlet standard for web applications.
1.
Configuring
and Mapping a Servlet
2.
Servlet
Init Parameters
3.
Servlet
Load-on-Startup
4.
Context
Parameters
Configuring and Mapping a Servlet
To configure a servlet in the web.xml file, you
write this:
<?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>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>servletexample.createHelloWorldExample
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorldExample/*</url-pattern>
</servlet-mapping>
</web-app>
Servlet-mapping
has two child tags, url-pattern and servlet-name. url-pattern specifies the
type of urls for which, the servlet given in servlet-name should be called. Be
aware that, the container will use case-sensitive for string comparisons for
servlet matching.
Servlet Init Parameters
You can pass parameters to a servlet from the
web.xml file. The init parameters of a servlet can only be accessed by that
servlet. Here is how you configure them in the web.xml file:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>servletexample.createHelloWorldExample
</servlet-class>
<init-param>
<param-name>myParam</param-name>
<param-value>paramValue</param-value>
</init-param>
</servlet>
Here is how you read the init parameters from inside
your servlet - in the servlets init() method:
public
class createHelloWorldExample extends
GenericServlet {
protected String myParam = null;
public void init(ServletConfig servletConfig)
throws ServletException{
this.myParam =
servletConfig.getInitParameter("myParam");
}
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException {
response.getWriter().write("<html><body>myParam =
" +
this.myParam +
"</body></html>");
}
}
A servlets init() method is called when
the servlet container loads the servlet for the first time. No one can access
the servlet until the servlet has been loaded, and the init() method
has been called successfully.
Servlet Load-on-Startup
The <servlet> element has a
subelement called <load-on-startup> which you can use to
control when the servlet container should load the servlet. If you do not
specify a <load-on-startup> element, the servlet container will
typically load your servlet when the first request arrives for it.
By setting a <load-on-startup> element,
you can tell the servlet container to load the servlet as soon as the servlet
container starts. Remember, the servlets init() method is called when
the servlet is loaded.
Here is an example <load-on-startup> configuration:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>servletexample.createHelloWorldExample
</servlet-class>
<init-param><param-name>container.script.static</param-name>
<param-value>/WEB-INF/container.script</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The number inside the <load-on-startup>1</load-on-startup> element
tells the servlet container in what sequence the servlets should be loaded. The
lower numbers are loaded first. If the value is negative, or unspecified, the
servlet container can load the servlet at any time.
Context Parameters
You can also set some context parameters which can
be read from all servlets in your application. Here is how you configure a context
parameter:
<context-param>
<param-name>myParam</param-name>
<param-value>the value</param-value>
</context-param>
Here is how you access the parameter from inside an HttpServlet subclass:
String myContextParam
=request.getSession().getServletContext().getInitParameter("myParam");
Web applications use a standard directory structure defined in the Java EE specification. ... The WEB-INF directory contains the deployment descriptors for the Web application ( web.xml and weblogic.xml ) and two subdirectories for storing compiled Java classes and library JAR files.
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
java training in chennai |
java training institutes in chennai