Tomcat Web application using web.xml control Xiangjie (1)

sponsored links
Transfer from: http://fanqiang.chinaunix.net/app/web/2006-04-29/4122.shtml

Tomcat Web application using web.xml control Xiangjie (1)
Study: feng_sundy Source: unknown (2006-04-29 13:39:34)

A definition of the head and the root element

Deployment descriptor file, like all XML documents, must begin with an XML header. The first statement can use the XML version and character encoding given file.
DOCYTPE statement must appear immediately after this first. This statement tells the server for the servlet specification version (such as 2.2 or 2.3) and designate the rest of the contents of this file syntax DTD (Document Type Definition, Document Type Definition).
All top-level deployment descriptor file (root) element for the web-app. Please note, XML elements like HTML, they are case-sensitive. Therefore, web-App and WEB-APP is illegal, web-app must be lowercase.

2 deployment descriptor file order of elements within

XML element is not only a case-sensitive, and they also appear in other elements in the order of sensitivity. For example, XML file header must be the first, DOCTYPE declaration must be second, while the web-app element must be a third. In the web-app element, the element of order is also important. The server does not enforce the requirement that such order, but they allow (in fact some of the server is doing) is completely incorrect to refuse the order of the elements containing the Web application. This means that the use of non-standard elements in the order of the web.xml file is not portable.
The following list shows all the direct web-app element in the legal elements necessary for the order. For example, this list shows servlet element must appear before all the servlet-mapping element. Please note that all of these elements are optional. Therefore, an element can be omitted, but can not put it in the incorrect location.
l icon icon element that IDE and GUI tools for Web applications that one and two image files.
l display-name display-name element provides GUI tools may be used to tag this particular Web application a name.
l description description element is given a descriptive text related to this.
l context-param context-param element declaration within the scope of application initialization parameter.
l filter element will filter a name and an implementation class associated javax.servlet.Filter interface.
l filter-mapping, once named a filter, filter-mapping element must use it with one or more of the servlet or JSP page is associated.
l listener servlet API version 2.3 adds support for the event listener, event listener in the create, modify and delete a session or be notified when the servlet environment. Listener element that the event listener class.
l servlet or JSP page to servlet initialization parameters, or develop custom URL, you must first name the servlet or JSP page. Servlet element is used to complete this task.
l servlet-mapping server, usually provide a default servlet URL: http://host/webAppPrefix/servlet/ServletName. However, often changes the URL, so that servlet can access the initialization parameters, or to more easily deal with the relative URL. When you change the default URL, use the servlet-mapping element.
l session-config If a session has not been visited within a certain time, the server can discard it in order to save memory. By using the HttpSession of setMaxInactiveInterval method clearly set out the value of a single session object, or session-config element can be used to develop the default timeout value.
l mime-mapping Web applications have thought if a special file, hoping to ensure their distribution to specific MIME type, mime-mapping element to provide that assurance.
l welcom-file-list welcome-file-list element indicates that the server referenced in the receipt of a directory name instead of file name URL, which file to use.
l error-page error-page element makes it back to a specific HTTP status code, or a specific type of exception is thrown, can develop the page to be displayed.
l taglib taglib element of the tag library descriptor file (Tag Libraryu Descriptor file) specified alias. This feature allows you to change the TLD file's location, rather than edit the JSP pages to use these files.
l resource-env-ref resource-env-ref element declarations and resources related to a management object.
l resource-ref resource-ref element declares a resource factory to use external resources.
l security-constraint security-constraint element of the development should protect the URL. It is with the login-config element in combination
l login-config with the login-config element to specify how the server to attempt to access a protected page of the user authorization. With the sercurity-constraint element is used in combination.
l security-role security-role element is given a list of security roles, these roles will appear in the servlet element in the security-role-ref element in the role-name child element. Statement separately the role of IDE can handle high-level security information easier.
l env-entry env-entry element declaration environment entries Web applications.
l ejb-ref ejb-ref element declares a reference to EJB's home directory.
l ejb-local-ref ejb-local-ref element declares a EJB's local home directory of the application.

3 distribution name and custom UL

Completed in the web.xml one of the most common task is the name given servlet or JSP pages and custom URL. Distribution of elements with the servlet name, servlet-mapping element will use the custom URL with just the name of the associated distribution.
3.1 The distribution of names in order to provide initialization parameters to the servlet or JSP page to define a custom URL, or assign a security role, you must first servlet or JSP page to a name. Servlet element can be assigned a name. The most common formats include servlet-name and servlet-class sub-elements (elements within the web-app), as follows:
<servlet>
<servlet-name> Test </ servlet-name>
<servlet-class> moreservlets.TestServlet </ servlet-class>
</ Servlet>

This means that in WEB-INF/classes/moreservlets/TestServlet the servlet has been registered name Test. To the servlet a name has two main implications. First, the initialization parameters, custom URL pattern, and other custom name instead of by the registered class name refers to this servlet. Next, the class name in the URL rather than the use of this name. Therefore, using the definition just given, URL http://host/webAppPrefix/servlet/Test for http://host/webAppPrefix/servlet/moreservlets.TestServlet place.
Remember: XML element is not only case sensitive, and define their order is also important. For example, web-app element of all elements must be in all servlet servlet-mapping element (the next section description) before, but also at 5.6 and 5.11 discuss or document with the associated filter element (if any) before. Similarly, servlet's servlet-name child element must appear before the servlet-class. 5.2 "deployment descriptor file elements within the order" will detail this essential order.
For example, Listing 5-1 shows a simple named TestServlet servlet, which resides in the moreservlets package. Because this servlet is rooted in a directory called deployDemo part of the Web application, so TestServlet.class on deployDemo / WEB-INF / classes / moreservlets in. Listing 5-2 shows will be placed in the deployDemo / WEB-INF / web.xml file within a part. This web.xml file to use servlet-name and servlet-class element Test the name associated with TestServlet.class. Figure 5-1 and Figure 5-2 are shown using the default URL and login name when calling TestServlet results.

Listing 5-1 TestServlet.java
package moreservlets;

import java.io. *;
import javax.servlet .*;
import javax.servlet.http .*;

/ ** Simple servlet used to illustrate servlet naming
* And custom URLs.
* <P>
* Taken from More Servlets and JavaServer Pages
* From Prentice Hall and Sun Microsystems Press,
* Http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
* /

public class TestServlet extends HttpServlet (
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException (
response.setContentType ("text / html");
PrintWriter out = response.getWriter ();
String uri = request.getRequestURI ();
out.println (ServletUtilities.headWithTitle ("Test Servlet") +
"<BODY BGCOLOR=\"#FDF5E6\"> \ n" +
"<H2> URI:" + uri + "</ H2> \ n" +
"</ BODY> </ HTML>");
)
)

Listing 5-2 web.xml (note the name of the servlet excerpt)
<? 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> Test </ servlet-name>
<servlet-class> moreservlets.TestServlet </ servlet-class>
</ Servlet>
<! - ... ->
</ Web-app>

3.2 The definition of a custom URL
Most servers have a default serlvet URL:
http://host/webAppPrefix/servlet/packageName.ServletName. Although the development is convenient to use the URL, but we often want another URL for the deployment. For example, you may need a Web application in the top level URL (eg, http://host/webAppPrefix/Anyname), and in this URL does not servlet entry. URL at the top simplifies the use of relative URL. In addition, for many developers, the top-level URL look longer and more cumbersome than the default URL shorter.
In fact, sometimes need to use the custom URL. For example, you might want to turn off the default URL mapping in order to better enforce security restrictions or to prevent users from accidentally access without initialization parameter servlet. If you ban a default URL, then how do you access servlet then? Then only use a custom URL.
To assign a custom URL, can be used servlet-mapping element and servlet-name and url-pattern sub-elements. Servlet-name element provides a random name, this name can be referenced using the corresponding servlet; url-pattern describes the Web application relative to the root directory of the URL. url-pattern element value must be a slash (/) start.
The following extract gives a simple web.xml, which allows the use of URL http://host/webAppPrefix/UrlTest not http://host/webAppPrefix/servlet/Test or
http://host/webAppPrefix/servlet/moreservlets.TestServlet. Please note that still need to XML header, DOCTYPE declaration and the element web-app closed. In addition, can recall, XML elements appear in the order is not arbitrary. In particular, the need to put all the servlet element before all the servlet-mapping element.
<servlet>
<servlet-name> Test </ servlet-name>
<servlet-class> moreservlets.TestServlet </ servlet-class>
</ Servlet>
<! - ... ->
<servlet-mapping>
<servlet-name> Test </ servlet-name>
<url-pattern> / UrlTest </ url-pattern>
</ Servlet-mapping>
URL patterns can include wildcards. For example, the following small program instructs the server to send all the Web application URL prefix to the beginning to the end .. asp named BashMS request to the servlet.
<servlet>
<servlet-name> BashMS </ servlet-name>
<servlet-class> msUtils.ASPTranslator </ servlet-class>
</ Servlet>
<! - ... ->
<servlet-mapping>
<servlet-name> BashMS </ servlet-name>
<url-pattern> / *. asp </ url-pattern>
</ Servlet-mapping>
3.3 JSP page named because the JSP page to be converted into sevlet, natural that the same name as named JSP page servlet. After all, JSP pages may be from the initialization parameters, security settings or customize the URL in the benefit, as do ordinary serlvet. Although the JSP page servlet that the background is actually saying is correct, but there is a critical suspicion: that is, you do not know the actual JSP page class name (the name chosen because the system itself). Therefore, in order to name the JSP page, you can replace the jsp-file element servlet-calss elements, as follows:
<servlet>
<servlet-name> Test </ servlet-name>
<jsp-file> / TestPage.jsp </ jsp-file>
</ Servlet>
Reasons named JSP page servlet named exactly the same reason: that in order to provide a custom setting (eg, initialization parameters and security settings) with the use of the name, and in order to change the activation JSP page URL (for example, so many pages to deal with through the same URL, or removed from the URL. jsp extension). However, in the setting initialization parameters, it should be noted, JSP page is to use jspInit method, instead of init method reads the initialization parameters.
For example, Listing 5-3 shows a simple JSP page called TestPage.jsp, it is only used to activate it to print out the URL of the local part. TestPage.jsp deployDemo applications placed in the top. Listing 5-4 shows used to be assigned a login name PageName, and this registration form name and http://host/webAppPrefix/UrlTest2/anything URL associated web.xml file (that is, deployDemo/WEB- INF / web.xml) part.

Listing 5-3 TestPage.jsp
<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.0 Transitional / / EN">
<HTML>
<HEAD>
<TITLE>
JSP Test Page
</ TITLE>
</ HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2> URI: <% = request.getRequestURI ()%> </ H2>
</ BODY>
</ HTML>

Listing 5-4 web.xml (note named JSP page extracts)
<? 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> PageName </ servlet-name>
<jsp-file> / TestPage.jsp </ jsp-file>
</ Servlet>
<! - ... ->
<servlet-mapping>
<servlet-name> PageName </ servlet-name>
<url-pattern> / UrlTest2 / * </ url-pattern>
</ Servlet-mapping>
<! - ... ->
</ Web-app>

4 against Activator servlet

Servlet or JSP page on the establishment of custom URL for one of the reasons is that doing so can be registered from the init (servlet) or jspInit (JSP page) method to obtain initialization parameter is read. However, the initialization parameter is used only in custom URL pattern or register to access servlet or JSP page name can be used with the default URL http://host/webAppPrefix/servlet/ServletName visit can not be used. Therefore, you may want to turn off the default URL, so no one accidentally calls the servlet initialization. This process is sometimes referred to as prohibited activator servlet, because most servers have a default servlet URL with the registration standard servlet, and activate the practical application of the default URL servlet.
There are two against the default URL of the main methods:
l in each Web application to re-mapping / servlet / model.
l Global Close Activator servlet.
Is important to note that although the re-mapping for each Web application in the / servlet / servlet mode activation than a total ban on the work done more, but the re-mapping can be a completely portable way to accomplish. On the contrary, global ban Activator servlet is completely machine-specific, in fact, some servers (such as ServletExec) did not have that option. The following discussion of each Web application re-mapping / servlet / URL mode strategy. Behind the global ban in the Tomcat servlet details of the activation device.
4.1 The re-mapping / servlet / URL pattern in a particular Web application start URL prohibit http://host/webAppPrefix/servlet/ treatment is very simple. Need to do is to create an error message servlet, and discussed in the previous section using the url-pattern element matches the request will be turned all the servlet. Simply use:
<url-pattern> / servlet / * </ url-pattern>
As a servlet-mapping element in the model can be.
For example, Listing 5-5 shows the SorryServlet servlet (Listing 5-6) and the beginning of the URL for all to http://host/webAppPrefix/servlet/ associated part of the deployment descriptor file.

Listing 5-5 web.xml (note JSP page named extract)
<? 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> Sorry </ servlet-name>
<servlet-class> moreservlets.SorryServlet </ servlet-class>
</ Servlet>
<! - ... ->
<servlet-mapping>
<servlet-name> Sorry </ servlet-name>
<url-pattern> / servlet / * </ url-pattern>
</ Servlet-mapping>
<! - ... ->
</ Web-app>

Listing 5-6 SorryServlet.java
package moreservlets;

import java.io. *;
import javax.servlet .*;
import javax.servlet.http .*;

/ ** Simple servlet used to give error messages to
* Users who try to access default servlet URLs
* (Ie, http://host/webAppPrefix/servlet/ServletName)
* In Web applications that have disabled this
* Behavior.
* <P>
* Taken from More Servlets and JavaServer Pages
* From Prentice Hall and Sun Microsystems Press,
* Http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
* /

public class SorryServlet extends HttpServlet (
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException (
response.setContentType ("text / html");
PrintWriter out = response.getWriter ();
String title = "Invoker Servlet Disabled.";
out.println (ServletUtilities.headWithTitle (title) +
"<BODY BGCOLOR=\"#FDF5E6\"> \ n" +
"<H2>" + Title + "</ H2> \ n" +
"Sorry, access to servlets by means of \ n" +
"URLs that begin with \ n" +
"Http://host/webAppPrefix/servlet/ \ n" +
"Has been disabled. \ N" +
"</ BODY> </ HTML>");
)

public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException (
doGet (request, response);
)
)

4.2 The global ban Activator: Tomcat
Tomcat 4 in the methods used to turn off the default URL with Tomcat 3 used in the different treatment. Both methods are described below:
1. Prohibition Activator: Tomcat 4
Tomcat 4 with the same method with the previous close activator servlet, namely the use of web.xml in the url-mapping element to close. The difference is that Tomcat used on the install_dir / conf a server dedicated to the global web.xml file, which is used in front of each Web application stored in the WEB-INF directory in the standard web.xml file.
Therefore, in order to activate the device to turn off Tomcat 4 servlet, just in install_dir / conf / web.xml to make a simple comment out the / servlet / * URL mapping entry can be as follows:
<! -
<servlet-mapping>
<servlet-name> invoker </ servlet-name>
<url-pattern> / servlet / * </ url-pattern>
</ Servlet-mapping>
->
Once again, it should be noted this item is in stored in install_dir / conf of the Tomcat-specific web.xml file, this file is not stored in each Web application's WEB-INF directory in the standard web.xml.
2. Prohibition Activator: Tomcat3
In the Apache Tomcat version 3, by install_dir / conf / server.xml in the Global Notes for the Prohibition of the default item InvokerInterceptor servlet URL. For example, the following are prohibited to use the default servlet URL as part of the server.xml file.
<! -
<RequsetInterceptor
className = "org.apache.tomcat.request.InvokerInterceptor"
debug = "0" prefix = "/ servlet /" />
->

5 initialization and pre-load servlet with JSP page

Discussed here control servlet and JSP pages start behavior. In particular, illustrates how the distribution of initialization parameters and how to change the server load servlet and JSP midterm survival time of the page.
5.1 The distribution of servlet initialization parameters to use servlet init-param element to provide initialization parameters, init-param param-name element and with param-value sub-elements. For example, in the following example, if initServlet servlet is to use its registered name (InitTest) visit, it will be able to call its methods getServletConfig (). GetInitParameter ("param1") was "Value 1", call getServletConfig ( ). getInitParameter ("param2") was "2."
<servlet>
<servlet-name> InitTest </ servlet-name>
<servlet-class> moreservlets.InitServlet </ servlet-class>
<init-param>
<param-name> param1 </ param-name>
<param-value> value1 </ param-value>
</ Init-param>
<init-param>
<param-name> param2 </ param-name>
<param-value> 2 </ param-value>
</ Init-param>
</ Servlet>
Initialization parameters are involved, there are several points to note:
l return value. GetInitParameter the return value is always a String. Therefore, the previous example, can use Integer.parseInt param2 get a int.
l JSP in the initialization. JSP pages use jspInit not init. JSP pages also need to use the jsp-file element instead of servlet-class.
l default URL. Initialization parameters only by their registered name or registered name associated with their custom URL patterns can be used to access Servlet. Thus, in this example, param1 and param2 will be able to use the initialization parameters when the URL http://host/webAppPrefix/servlet/InitTest available, but the use of URL http://host/webAppPrefix/servlet/myPackage.InitServlet When you can not use.
For example, Listing 5-7 shows a simple named InitServlet servlet, it uses the init method to set firstName and emailAddress fields. Listing 5-8 shows the name assigned to the servlet's web.xml file InitTest.
Listing 5-7 InitServlet.java
package moreservlets;

import java.io. *;
import javax.servlet .*;
import javax.servlet.http .*;

/ ** Simple servlet used to illustrate servlet
* Initialization parameters.
* <P>
* Taken from More Servlets and JavaServer Pages
* From Prentice Hall and Sun Microsystems Press,
* Http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
* /

public class InitServlet extends HttpServlet (
private String firstName, emailAddress;

public void init () (
ServletConfig config = getServletConfig ();
firstName = config.getInitParameter ("firstName");
emailAddress = config.getInitParameter ("emailAddress");
)

public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException (
response.setContentType ("text / html");
PrintWriter out = response.getWriter ();
String uri = request.getRequestURI ();
out.println (ServletUtilities.headWithTitle ("Init Servlet") +
"<BODY BGCOLOR=\"#FDF5E6\"> \ n" +
"<H2> Init Parameters: </ H2> \ n" +
"<UL> \ N" +
"<LI> First name:" + firstName + "\ n" +
"<LI> Email address:" + emailAddress + "\ n" +
"</ UL> \ n" +
"</ BODY> </ HTML>");
)
)

Listing 5-8 web.xml (note initialization parameters of the extract)
<? 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> InitTest </ servlet-name>
<servlet-class> moreservlets.InitServlet </ servlet-class>
<init-param>
<param-name> firstName </ param-name>
<param-value> Larry </ param-value>
</ Init-param>
<init-param>
<param-name> emailAddress </ param-name>
<param-value> Ellison@Microsoft.com </ param-value>
</ Init-param>
</ Servlet>
<! - ... ->
</ Web-app>

5.2 The distribution of initialization parameters to the JSP page JSP provides three different initialization parameters to provide initialization parameters to the servlet.
1) Use jsp-file instead of servlet-class. Therefore, WEB-INF/web.xml file servlet element as follows:
<servlet>
<servlet-name> PageName </ servlet-name>
<jsp-file> / RealPage.jsp </ jsp-file>
<init-param>
<param-name> ...</ param-name>
<param-value> ...</ param-value>
</ Init-param>
...
</ Servlet>
2) almost always assign a specific URL pattern. Of the servlet, generally corresponding to http://host/webAppPrefix/servlet/ begin to use the default URL. Just remember, use the registered name and not the original name. This JSP page is technically legal. For example, in the example given above, available URL http://host/webAppPrefix/servlet/PageName access RealPage.jsp the right initialization parameters have access version. However, for the JSP page, many users did not seem to like using the conventional servlet's URL. In addition, if the JSP page in the server providing the directory list directory (for example, a neither nor index.jsp index.html files), the user may connect to this JSP page, click on it, so accidental activation of an uninitialized page. Therefore, the good way is to use the url-pattern (5.3 section) to the original JSP page URL and registered servlet name is associated. In this way, the client can use the common name of the JSP page, but still activate the customized version. For example, given the servlet from the definition of item 1, you can use the following servlet-mapping definition:
<servlet-mapping>
<servlet-name> PageName </ servlet-name>
<url-pattern> / RealPage.jsp </ url-pattern>
</ Servlet-mapping>
3) JSP page using jspInit not init. Automatically created from the JSP page servlet may have used the inti method. Therefore, the use of init method of a JSP declaration is illegal, must be developed jspInit method.
To illustrate the initialization process JSP pages, Listing 5-9 shows the JSP page called InitPage.jsp, it contains a jspInit method and deployDemo Web applications placed on the top of the hierarchy. General, http://host/deployDemo/InitPage.jsp form of URL to activate this page does not have access to the version of the initialization parameters, which will be variable firstName and emailAddress display null. However, web.xml file (Listing 5-10) assigned a login name and the registered name and the URL pattern / InitPage.jsp associated.

Listing 5-9 InitPage.jsp
<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.0 Transitional / / EN">
<HTML>
<HEAD> <TITLE> JSP Init Test </ TITLE> </ HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2> Init Parameters: </ H2>
<UL>
<LI> First name: <% = firstName%>
<LI> Email address: <% = emailAddress%>
</ UL>
</ BODY> </ HTML>
<%!
private String firstName, emailAddress;

public void jspInit () (
ServletConfig config = getServletConfig ();
firstName = config.getInitParameter ("firstName");
emailAddress = config.getInitParameter ("emailAddress");
)
%>

Listing 5-10 web.xml (JSP page init parameters shows an excerpt)
<? 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> InitPage </ servlet-name>
<jsp-file> / InitPage.jsp </ jsp-file>
<init-param>
<param-name> firstName </ param-name>
<param-value> Bill </ param-value>
</ Init-param>
<init-param>
<param-name> emailAddress </ param-name>
<param-value> gates@oracle.com </ param-value>
</ Init-param>
</ Servlet>
<! - ... ->
<servlet-mapping>
<servlet-name> InitPage </ servlet-name>
<url-pattern> / InitPage.jsp </ url-pattern>
</ Servlet-mapping>
<! - ... ->
</ Web-app>

5.3 to provide application initialization parameters within the general, a single servlet or JSP page to assign initialization parameters. Servlet or JSP page is specified using the getInitParameter method of ServletConfig read these parameters. However, in some cases, wish to provide any servlet or JSP page by using the getInitParameter method of ServletContext to read the system-wide initialization parameters.
Can use context-param element declaration within these systems the initialization value. context-param element should contain the param-name, param-value and an optional description sub-elements, as follows:
<context-param>
<param-name> support-email </ param-name>
<param-value> blackhole@mycompany.com </ param-value>
</ Context-param>
May recall, in order to guarantee portability, web.xml elements within the statement must be the correct order. But here it should be noted, context-param element must be related to any element of the document (icon, display-name or description) and after the filter, filter-mapping, listener, or before the servlet element.
5.4 load servlet when the server starts
If the servlet or JSP page takes a long time the implementation of the init (servlet) or jspInit (JSP) method. For example, if the init method or jspInit ResourceBundle lookup from a database or output. This case, the first client request, the default behavior of loading servlet client will first produce a long delay. Therefore, we can use servlet's load-on-startup element provides the first starts the server load servlet. Here is an example.
<servlet>
<servlet-name> ... </ servlet-name>
<servlet-class> ... </ servlet-class> <! - Or jsp-file ->
<load-on-startup/>
</ Servlet>
Body of this element can provide an integer instead of using an empty load-on-startup. Idea is that the server should load a larger number of servlet or JSP page is loaded before the servlet or JSP smaller number of pages. For example, the following servlet entry (placed in the Web application's WEB-INF directory of the web.xml file within the web-app element) will be directed to the server first loaded and initialized SearchServlet, then loaded and initialized from the Web at applications result in the index.jsp file directory generated servlet.
<servlet>
<servlet-name> Search </ servlet-name>
<servlet-class> myPackage.SearchServlet </ servlet-class> <! - Or jsp-file ->
<load-on-startup> 1 </ load-on-startup>
</ Servlet>
<servlet>
<servlet-name> Results </ servlet-name>
<servlet-class> / results / index.jsp </ servlet-class> <! - Or jsp-file ->
<load-on-startup> 2 </ load-on-startup>
</ Servlet>

6 Declaration filter

servlet version 2.3 introduces the concept of filters. While all support the servlet API version 2.3 servers support filters, but in order to use the filter element must be used in the web.xml version 2.3 of the DTD.
Filters can intercept and modify into a servlet or JSP page request or a servlet or JSP page from the corresponding issue. In the implementation of a servlet or JSP page, you must first related to the implementation of the doFilter method of filter. In the filter object calls its doFilter FilterChain, the implementation of the chain to the next filter. If no other filter, servlet or JSP page is executed. Filter with the arrival of the ServletRequest object on all the access, so they can see the client name, find the arrival of the cookie, etc.. Servlet or JSP page to access the output of the filter response object can be wrapped in a stand-in object (stand-in object) in, for example, the cumulative output to a buffer. FilterChain object in the doFilter method is called after the filter can check the buffer, if necessary, modify it, and then sent to the client.
For example, empire difficult Listing 5-11, a simple filter, as long as the servlet or JSP to access the relevant page, it will intercept the request and print a report to standard output (the development process running on the desktop system, most server can use the filter).

Listing 5-11 ReportFilter.java
package moreservlets;

import java.io. *;
import javax.servlet .*;
import javax.servlet.http .*;
import java.util .*;

/ ** Simple filter that prints a report on the standard output
* Whenever the associated servlet or JSP page is accessed.
* <P>
* Taken from More Servlets and JavaServer Pages
* From Prentice Hall and Sun Microsystems Press,
* Http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
* /

public class ReportFilter implements Filter (
public void doFilter (ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException (
HttpServletRequest req = (HttpServletRequest) request;
System.out.println (req.getRemoteHost () +
"Tried to access" +
req.getRequestURL () +
"On" + new Date () + ".");
chain.doFilter (request, response);
)

public void init (FilterConfig config)
throws ServletException (
)

public void destroy () ()
)

Once a filter that can filter element in the web.xml and the use of filter-name (any name), file-class (fully qualified class name) and (optional) init-params statement of its child elements. Please note that elements in the web.xml of the web-app element in the emergence of order is not arbitrary; allows the server (but not required) enforce the order required, and some of the server is actually doing. But here to note that all the filter element must be before any filter-mapping element, filter-mapping element must also appear in all servlet or servlet-mapping element before.
For example, given the above ReportFilter class can be made in the web.xml, the following filter declaration. It is the name of the Reporter and the actual class ReportFilter (in moreservlets package) is associated.
<filter>
<filter-name> Reporter </ filter-name>
<filter-class> moresevlets.ReportFilter </ filter-class>
</ Filter>
Once the name of a filter can be used to filter-mapping element to it with one or more of the servlet or JSP page is associated. There are two options on this work.
First of all, you can use filter-name and servlet-name child element to this filter with a specific servlet name (this servlet name must be later in the same web.xml file to use servlet element declarations) association. For example, the following snippet indicating system as long as the use of a custom URL to access the servlet or JSP named SomeServletName page, on the run named Reporter of the filter.
<filter-mapping>
<filter-name> Reporter </ filter-name>
<servlet-name> SomeServletName </ servlet-name>
</ Filter-mapping>
Second, the available filter-name and url-pattern sub-elements of the filter with a servlet, JSP page or static content associated. For example, the physiognomy of the program fragment indicating system as long as access to any Web application URL, on the run named Reporter of the filter.
<filter-mapping>
<filter-name> Reporter </ filter-name>
<url-pattern> / * </ url-pattern>
</ Filter-mapping>
For example, Listing 5-12 shows the filter will ReportFilter PageName with the name of the servlet web.xml file associated with the part. Name PageName turn, and the JSP page called TestPage.jsp as well as the beginning of the URL pattern http://host/webAppPrefix/UrlTest2/ associated. TestPage.jsp JSP page source code have been named in the preceding three to talk about "distribution names and custom URL" given. In fact, Listing 5-12 in the servlet and servlet-name items intact from the festival to take them there. Given the web.xml entries, you can see the following standard form of debugging output report (wrapped for easy reading).
audit.irs.gov tried to access
http://mycompany.com/deployDemo/UrlTest2/business/tax-plan.html
on Tue Dec 25 13:12:29 EDT 2001.

Listing 5-12 Web.xml (note filter usage extract)
<? 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>
<filter>
<filter-name> Reporter </ filter-name>
<filter-class> moresevlets.ReportFilter </ filter-class>
</ Filter>
<! - ... ->
<filter-mapping>
<filter-name> Reporter </ filter-name>
<servlet-name> PageName </ servlet-name>
</ Filter-mapping>
<! - ... ->
<servlet>
<servlet-name> PageName </ servlet-name>
<jsp-file> / RealPage.jsp </ jsp-file>
</ Servlet>
<! - ... ->
<servlet-mapping>
<servlet-name> PageName </ servlet-name>
<url-pattern> / UrlTest2 / * </ url-pattern>
</ Servlet-mapping>
<! - ... ->
</ Web-app>

(Http://www.fanqiang.com)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Tomcat Web application using web.xml control Xiangjie (1)

  • lighttpd + fastcgi built a server how to preserve the status of server

    Requirements: server-side has much static files, there is much use of c + + to prepare the operational components necessary to carry out these functions on the external packaging Published Service, server-side state required to preserve the client (simila

  • Maven 2.0: Compile. Test. Deployment. Run

    <url> http://maven.apache.org </ url> <dependencies> <dependency> <groupId> junit </ groupId> <artifactId> junit </ artifactId> <version> 3.8.1 </ version> <scope> test </ scope> <

  • Openfire Plugin Developer's Guide

    Introduction Openfire features plug-ins are enhanced. This document is a guide to developers to create plug-ins. The structure of a plug-in Plug-ins Plug-ins openfireHome stored in the directory. When deploying a plug-in jar or war file, it will automatic

  • RoR explained

    ROR is Ruby on Rails. Ruby is a well-known has been very good dynamic language It's dynamic language. Simple and easy. Dynamic languages are interpreted, but the performance may make a discount, but not absolute, because the application is complex, th

  • Memo hibernate problem

    hibernate using myeclipse automatically generated HBM mapping file will be schema ="***" catalog ="***" Separately are the database instance name and database name, but in fact has been stated jdbc url connection database, are the ...

  • Process migration from tomcat to websphere changes

    Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm's

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries