1 Defining the header and root element deployment descriptor files like all XML files must start with an XML header. This header declares can use the XML version and give the character encoding of the file. DOCYTPE declaration must immediately appear after this header. This declaration tells the server to which the version of the servlet specification (such as 2.2 or 2.3) is specified and specifying the DTD (Document Type Definition, document type definition) of the syntax of this file. The top (root) element of all deployment descriptor files is a web-app. Note that XML elements are unlike HTML, they are sensitive. Therefore, both web-app and web-app are illegal, and the web-app must be written. 2 Deployment Descriptor Document The XML element is not only sensitive, but also they are sensitive to the order in other elements. For example, the XML head must be the first item in the file, and the DOCTYPE declaration must be the second item, and the web-app element must be the third item. In the web-app element, the order of the elements is also important. The server is not necessarily forced to require this order, but they allow (actually some servers doing this) completely refuse to perform web applications that contain elements in order incorrect. This means that the Web.xml file used in the order of the non-standard element is unmistable. The following list gives all the order necessary for all legitimate elements that can appear in the web-app element. For example, this list Description servlet elements must appear before all servlet-mapping elements. Please note that all of these elements are optional. Therefore, a certain element can be omitted, but it cannot be placed in an incorrect position. l icon icon elements indicate that the IDE and GUI tools are used to represent one and two image files of the web application. l Display-name Display-Name element provides a GUI tool that may be used to mark a name for this particular web application. The L Description Description element gives an illustrative text associated with this. l Context-param context-param element declares initialization parameters within the application range. l Filter filter elements Associate a name with a class that implements the Javax.Servlet.Filter interface. l Filter-Mapping Once a filter is named, use the filter-mapping element to associate it with one or more servlets or JSP pages. l Listener Servlet API version 2.3 adds support for event listeners, and event listeners are notified when establishing, modifying, and deleting sessions or servlet environments. The Listener element indicates the event listener class. l Servlet When you set an initialization parameter or a custom URL to the servlet or JSP page, you must first name the servlet or JSP page. The servlet element is used to complete this task. l Servlet-Mapping Server typically provides a default URL for servlet:
http:// host / webappppprefix / servlet / servletname
. However, this URL is often changed so that servlets can access initialization parameters or more easily handle relative URLs. Using servlet-mapping elements when changing the default URL. l Session-config If a session is not accessed for a certain period of time, the server can discard it to save memory. The timeout value of a single session object can be explicitly set by using the HttpSession's setMaxinactiveInterVal method, or the default timeout value can be used to use the session-config element. l Mime-mapping If the web application has a special file, I hope to guarantee the assigned MIME type, then the mime-mapping element provides this guarantee. l Welcom-file-list welcome-file-list element indicates which file when receiving a directory name instead of a file name instead of a file name. l Error-Page Error-Page element allows the page that will be displayed when the particular type of abnormality is returned, or when the particular type of exception is thrown. l Taglib Taglib Elements Specify an alias for tag libraryu descriptor file. This feature allows you to change the location of the TLD file without editing the JSP page using these files. l Resource-Env-Ref Resource-Env-Ref element declares a management object related to the resource. l Resource-Ref Resource-Ref element declares an external resource used by a resource factory. l Security-constraint security-constraint Elements Develop the URL that should be protected. It combines L login-config elements to specify how to use the login-config element to specify how the server is authorized to access the protected page to try to access the protected page. It is used in combination with the Sercurity-Constraint element. l Security-role security-role elements give a list of secure roles that will appear in the Role-Name sub-elements of the security-role-ref elementality within the servlet element. Declaration of the role separately makes advanced IDE processing security information easier. l Env-entry env-entry elements declare the environment item of the web application. l EJB-REF EJB-REF Elements Declare a reference to the primary directory of an EJB. l Ejb-local-ref ejb-local-ref element declares an application of an EJB local host directory. 3 Assign Names and customized ULs One of the most common tasks completed in Web.xml is to give the servlet or JSP page to the name and custom URL. Use the servlet element to associate the custom URL with the just assigned name using the servlet-mapping element. 3.1 Assignment Name In order to provide initialization parameters, define a custom URL or assign a security role to the servlet or JSP page, you must first give the servlet or JSP page a name. A name can be assigned via the servlet element. The most common formats include servlet-name and servlet-class sub-elements (within a web-app element), as shown below:
Therefore, using the definition just given, urlhtp: // host / webappprefix / servlet / test
Can be used as
http://host/webappprefix/servlet/moreServlets.TestServlet
Site. Keep in mind: XML elements are not only sensitive, but also define their order. For example, all servlet elements in the web-app element must be in all servlet- mapping elements (introduced by the next section), and also discussed in Section 5.6 and 5.11 (if any) prior to. Similarly, servlet-name child elements of servlet must also appear before servlet-class. Section 5.2 "Deployment Descriptor Document" The order of elementality will be described in detail. For example, the program list 5-1 gives a simple servlet called TestServlet, which resides in the MoreServlets package. Because this servlet is part of a web application in a directory called DeployDemo, TestServlet.class is placed in DeployDemo / Web-INF / CLASSES / MORESERVLETS. Program Listing 5-2 gives a part of the web.xml file that will be placed in the deploydedeno / web-INF /. This web.xml file uses servlet-name and servlet-Class elements to associate name Test with TestServlet.class. Figures 5-1 and Figure 5-2 show the results when using the default URL and the registration name call TestServlet, respectively. Listing 5-1 TestServlet.javapackage 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" "
/ n" "">
http://host/webapprefix/servlet/packagename.servletname
. Although it is very convenient to use this URL in development, we often want another URL to deploy. For example, a URL that appears in a web application is required (eg Http: // Host / WebAppreFix / Anyname), and there is no servlet item in this URL. The URL located on the top layer simplifies the use of relative URLs. In addition, for many developers, the top URL looks more shorter than the longer and more troublesome default URL. In fact, sometimes you need to use a custom URL. For example, you may want to close the default URL mapping to better enforce secure restrictions or prevent users from accidentally accessing a servlet without initialization parameters. If you prohibit the default URL, how do you access servlet? At this time, only the custom URL is used. To assign a custom URL, servlet-mapping elements can be used and its servlet-name, and url-pattern child elements. The servlet- name element provides an arbitrary name that can be used to reference the corresponding servlet; URL-Pattern describes the URL of the root directory relative to the web application. The value of the URL-Pattern element must be started in a slash (/). Let's give a simple web.xml excerpt, which allows URLHTTP: // Host / WebAppprefix / URLTest
Instead of
http:// host / webapppprefix / servlet / test
Or http: //host/webapprefix/servlet/moreServlets.TestServlet. Note that the XML header, DOCTYPE declaration, and web-app are closed elements. In addition, you can recall, the XML element is not casual. In particular, you need to put all servlet elements before all servlet-mapping elements.
Http: // host / webappppppprefix / urltest2 / anything in the form of a URL associated web.xml file (ie, deploydemetero / web-inf / web.xml). Program List 5-3 TestPage.jsp
http://java.sun.com/dtd/web-app_2_3.dtd
">
http:// host / webappppprefix / servlet / servletname
Cannot be used when access. Therefore, you may want to turn off the default URL, so no one will accidentally call the initialization servlet. This process is sometimes referred to as forbidden activator servlet because most of the server has a standard servlet registered with the default servlet URL and activates the default URL application's actual servlet. There are two main methods for prohibiting this default URL: l Remap / servlet / mode in each web application. l Global Close the activator servlet. It is important to note that although rereading / servlet / mode in each web application is more done than thoroughly activating Servlet, re-mapping can be done with a fully portable manner. Instead, the global prohibition activator servlet is completely for specific machines, and the actual server (such as servletexec) does not have such a selection. The following discussion strategy for re-maping / servlet / URL schema for each web application. The details of the global disable activator servlet in Tomcat are provided later. 4.1 Remap / Servlet / URL Mode Forbidden in a specific web application, http: // host / webapppppprefix / servlet /
The process of the beginning of the URL is very simple. What you need is to create an error message servlet and turn all match requests to the servlet using the URL-Pattern element discussed by the previous section. As long as you simply use:
http:// host / webapppprefix / servlet /
The beginning of the URL associated with the deployment descriptor file. Program List 5-5 Web.xml (Description JSP page Named Extract) XML Version = "1.0" Encoding = "ISO-8859-1">
http://java.sun.com/dtd/web-app_2_3.dtd
">
http:// host / webappppprefix / servlet / servletname
* in Web Applications That Have Disabled this * Behavior. *
* 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
http:// host / webapppprefix / servlet /
/ N " " has been disabled./n " " BODY> HTML> ");} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response);}} 4.2 Global Prohibit Activator: Tomcattomcat 4 is used to close the default URL is very different from Tomcat 3. The two methods are introduced below: 1. Prohibit Activator: Tomcat 4Tomcat 4 Use the same way as the previous way to close The activator servlet is turned off with the URL-MAPPING element in Web.xml. Differently in Tomcat uses a server-specific global web.xml file dedicated in install_dir / confation, and the previous use is stored every Standard web.xml file in a web-inf directory of a web application. So, in order to close the activator servlet in Tomcat 4, simply comment out / servlet / * URL mapping items in install_dir / conf / web.xml That is, as shown below:
It is available, but in use URL
http://host/webappprefix/servlet/mypackage.initservlet
Can not be used. For example, program list 5-7 gives a simple servlet called INITSERVLET, which uses the init method to set the firstname and emaildress fields. Program Listing 5-8 gives the allocated name inittest to the Web.xml file for servlet. Listing 5-7 InitServlet.javapackage moreservlets; import java.io *;. Import javax.servlet *;. Import javax.servlet.http *;. / ** Simple servlet used to illustrate servlet * initialization parameters *
. * 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.HEADWITHTILE ("init servlet" "
/ n" "">
Ellison@microsoft.com
param-value> init-param> servlet> web-app> 5.2 Allocation JSP Initialization Parameters Provide initialization parameters to the JSP page differ from three aspects Servlet provides initialization parameters. 1) Use JSP-File instead of servlet-class. Therefore, the servlet element of the web-inf / web.xml file is shown below:
The default URL began. Just remember, use the registration name instead of the original name. This is also legal for JSP pages. For example, in the examples given above, available URLs
http: // host / webapppprefix / servlet / PageName
Accessing RealPage.JSP has access to the initialization parameters. But when used in the JSP page, many users do not like to use the URL of the regular servlet. In addition, if the JSP page is located in the directory for the server to provide a directory list (eg, a directory that is neither index.html does not have an index.jsp file), the user may connect to this JSP page, click it, thus Accidentally activate uninitialized pages. Therefore, a good way is to use the URL-Pattern (5.3) to associate the original URL of the JSP page with the registered servlet name. In this way, the client can use the Normal name of the JSP page, but still activate the customized version. For example, given a servlet definition from item 1, you can use the following servlet-maping definition:
http://host/deploydemo/initpage.jsp
The form of URL will activate the version of this page does not have an initialization parameter access, thereby displays NULL for the firstName and EmailAddress variables. However, the web.xml file (Program List 5-10) assigns a registration name and then associates the registration name with the URL mode / ITPAGE.JSP. Program List 5-9 INTPAGE.Jsp
">
Gates@oracle.com
param-value> init-param> servlet>
param-value> context-param> Recalls, in order to ensure portability, the elements within Web.xml must be declared in the correct order. However, it should be noted here that the context-param element must arbitrarily arbitrarily after Icon, Display-Name or Description, and Filter, Filter-Mapping, Listener or Servlet elements. 5.4 Loading a servlet if the server is started, such as a servlet or JSP page has an init (servlet) or JSPinit (JSP) method for a long time. For example, if the init or jspinit method looks out from a database or resourcebundle. In this case, the default behavior of loading the servlet when the first client request will generate a longer period of time for the first client. Therefore, it is possible to use the servlet's Load-On-Startup element specify that the server loads the servlet during the first start. Below is an example.
In order to access the output of the Servlet or JSP page, the filter can wrap the response object in a stand-in object, and the output is accumulated to a buffer. After calling the Dofilter method of the FilterChain object, the filter checks the buffer. If necessary, it is modified, then transferred to the client. For example, a program list 5-11 empire is difficult to have a simple filter, just access the relevant servlet or JSP page, it intercepts the request and prints a report on the standard output (most of the development process is running on the desktop system, most This filter can be used by the server). Program List 5-11 ReportFilter.javaPackage MoreServlets; import java.io. *; Import javax.servlet. *; Import javax.servlet.http. *; Import java.util. *; / ** SIMPLE FILTER THATS A Report on . the standard output * whenever the associated servlet or JSP page is accessed *
* 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 is established, you can use the filter element and filter-name, file-class (fully qualified class name) in Web.xml. (Optional) INIT-Params sub-elements declare it. Please note that the order of the elements appear in the web-app element of Web.xml is not arbitrary; allowing the server (but not required) to force the desired order, and some of the actual servers do this. But here you should pay attention, all Filter elements must appear before any Filter-Mapping element, the filter-mapping element must appear before all servlets or servlet-mapping elements. For example, given the above ReportFilter class, the following Filter declaration can be made in Web.xml. It is associated with the actual class REPORTFILTER (in the MoreServlets package).
On Tue Dec 25 13:12:29 Edt 2001. Program List 5-12 Web.xml (Description Filter Usage) XML Version = "1.0" eNCoding = "ISO-8859-1?>
http://java.sun.com/dtd/web-app_2_3.dtd
">
Error-Code and Exception-Type use the Location element to indicate the corresponding URL. This URL must start. The page pointed out at the location indicated by the HTTPSERVLETREQUEST object can access information about the error, which two attributes are: javax.servlet.error.Status_code and javax.servlet.error.MESSAGE. Memories can be memo that the child elements of the web-app are declared in the correct order in Web.xml. As long as you remember, Error-Page appears near the end of the web.xml file, the servlet, servlet-name, and Welcome-file-List can be. 8.1 Error-Code Element In order to better understand the value of the error-code element, consider if the file name is incorrect, most sites will reflect. Doing so generally a 404 error message, it means that it cannot be found, but there is almost no more useful information. On the other hand, you can try it at www.microsoft.com
,
Www.ibm.com
Position or especially
Www.bea.com
Output unknown file names. This is a useful message that provides a selectable location in order to find the page of interest. Providing such useful error pages for web applications is worth it. In fact, RM-Error-Page Child Elements). The HTML form given by Form-Login-Page must have a J_Security_Check's action property, a username text field named J_USERNAME and a password field named J_Password. For example, program list 5-19 indicates that the server uses a form-based verification. A page named login.jsp in the top of the web application will collect usernames and passwords, and the failed login will be reported by the page of login-error.jsp in the same directory. Program List 5-19 Web.xml (Description Login-Config) XML Version = "1.0" Encoding = "ISO-8859-1">
http://java.sun.com/dtd/web-app_2_3.dtd
">
l Auth-constraint Although the web-resource-cololion element has been protected, the Auth-Constraint element indicates which users should have access to protected resources. This element should include one or more user category Role-Name elements that have access rights, and the Description element that contains (optional) a descriptive role. For example, the Security-Constraint element department in Web.XML specifies only access to the specified resource only by specifying an Administrator or Big Kahuna (or both). 9.3 Distribution Role Name So far, discussing the security issues that have been set to be completely processed by the container (server). But the servlet and the JSP page can also handle their own security issues. For example, the container may allow users to access a page that displays an additional close to the executive person from a BigWig or Bigchese role, but only the BigWig user is allowed to modify the parameters of this page. One common way to accomplish this more detailed control is to call the HTTPServletRequset's ISUSERINROLE method and modify the access. Servlet's security-role-ref child elements provide an alias that appears in the server's dedicated password file. For example, if a servlet called Request.isuserinRole ("Boss") is written, then the servlet is used in a server that calls role Manager Manager instead of BOSS. The following block allows the servlet to use any of these two names. Let's take an example: * 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 ContextReporter implements ServletContextListener {public void contextInitialized (ServletContextEvent event) {System.out.println ( "Context created on" new Date () ".") Public void contextDestroyed (ServletContextevent Event) {system.out.println ("Context Destroyed On" New Date () ".");}} Listing 5-21 Web.xml (declare an excerpt of a listener) XML Version = "1.0" Encoding = "ISO-8859-1">
"> http://java.sun.com/j2ee/j2ee-1_3-fr-spec.pdf