Features of each version of the servlet __servlet

Source: Internet
Author: User
Tags event listener locale

1. XML schema Definition Web query Deployment Profile

The version prior to Servlet 2.3 uses the DTD as the definition of the deployment description file, and its web.xml format is as follows:

<?xml version= "1.0" encoding= "Is0-8859-1"?>
<! DOCTYPE Web-app
Public "-//sunmicrosystems,inc.//dtd WebApplication 2.3f//en"
"Http://java.sun.com/j2ee/dtds/web-app_2.3.dtd" >
<web-app>
.......
</web-app>

The Servlet 2.4 Edition uses XML Schema definitions for the first time as a deployment description file, which makes it easier for web containers to verify web.xml syntax. At the same time, XML schemas provide a better extensibility, and the format in the Web.xml is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
xmlns:workflow= "Http://www.workflow.com"
Xmins:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
.........
</web-app>

Although the first few lines of the two versions appear to be different, the servlet version 2.4 program web.xml the Web artifacts in the same way as the servlet version 2.3.

2. Servlet Request Listener

The Servlet version 2.4 included the ServletRequest Listener in the event listener, including: Servletrequestlistener,servletrequestattributelistener and other related classes. These classes can be used to manage and control events related to ServletRequest actions. The following program shows the structure of a typical servletrequest listener.

Import Javax.setvlet.ServletContext;
Import Javax.servlet.ServletRequestListener;
Import Javax.servlet.ServletRequestAttributeListener;
Import Javax.servlet.http.HttpServletRequest;
Import iava.io.*;
Import Java.util.Locale;
Public final class Requestlistener implements Servletrequestlistener,
servletrequestattributelistener,servletcontextlistener{
........
public void requestlnitialized (Javax.servlet.ServletRequestEvent event) {
........
}
public void attributeadded (Javax.servlet.ServletRequestAttributeEvent event) {
........
}
public void Attributeremoved (Javax.servlet.ServletRequestAttributeEvent event) {
........
}
public void attributereplaced (Javax.servlet.ServletRequestAttributeEvent event) {
........
}
public void attributedestroyed (Javax.servlet.ServletRequestAttributeEvent event) {
........
}
}

3, Request dispatcher changes

The Servlet version 2.4 Web application enhances the fit functionality of the filter and request dispatcher, so that filters can conditionally filter Web requests based on the method used by the request dispatcher (requests dispatcher). The programmer can set the filter function by using elements in the Web.xml (Figure 1 below):


Figure 1 Filter for setting elements


• Only if the request comes directly from the customer, the filter takes effect and corresponds to the request condition.

• When request is transferred to a Web widget (by or definition) using the forward () method by a request distributor, the corresponding call forward condition.

• Similarly, when request is transferred to a Web widget using the Include () method by a request distributor, it is called an include condition.

• It is called an error condition when request is transferred to a Web widget by a request dispatcher using the error message page mechanism method.

• The condition of the fifth filter action can be a combination of the above four conditions.

The following program defines that when a client requests a/icsamples/* style URL, the security filter is used to filter the request. However, if the request to arrive at a Web widget with a URL of/icsamples/* is forwarded from a request dispatcher, the filter does not work.

<filter-mapping>
<filter-name>security filter</filter-name>
<url-pattern>/icsamples/*</url-pattern>
</filter-mapping>

If you use the following program settings, adding include,security filter in can only work if the include () method is used by a request dispatcher including Requestrecorderservlet. The security filter does not work in other cases, such as when the request is sent directly from the customer, or if dispatcher uses the forward method.

<filter-mapping>
<fliter-name>security filter</filter-name>
<servlet-name>RequestRecorderServlet</servlet-name>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>

The following program settings define security filter When request is sent directly by the customer or request

Dispatcher can work when using the forward method.

<filter-mapping>
<filter-name>security filter</filter-name>
<url-pattern>/icsamples/*</uri-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

Program Example 6 shows an example that is used in. The Reqdispatcherservlet defined in program 7 determines how requests are processed based on the parameter "type" requested by the user. In which two requestdispatcher (Rd, and Rd2) are used, they can forward requests to a servlet with a URL of/admin or/control. When the request is forwarded, the appropriate filter will work. The relationship between a filter and a Web resource can be defined in Web.xml. If the user enters a Web request parameter of "include", Reqdispatcherservlet's RequestDispatcher calls the include method so that Dispatcherfiltericd is used. Because the include is defined, the Resp.senderror () method is invoked if the user enters a Web request parameter of "ERROR" reqdispatcherservlet, so that Dispatcherfiltererr is used. Because in the definition of the error.

Example 6:

<filter-mapping>
<filter-name>DispatcherFilterIcd</filter-name>
<url-pattern>/admin</url-pattern>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>DispatcherFilterErr</filter-name>
<url-pattern>/errorpage</url-pattern>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

Example 7:

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

public class Reqdispatcherservlet extends HttpServlet
{
String Dispatchtype;
public void init (ServletConfig config) throws Servletexception
{
Super.init (config);
}
public void Service (HttpServletRequest Req,httpservletresponse resp)
Throws Servletexception,ioexception
{
PrintWriter Out=resp.getwriter ();
String type= "NONE";
if ((Req.getparameter ("type"))!=null)
{
Type = Req.getparameter ("type");
}
Resp.setcontenttype ("text/html");
Out.println ("<HTML>");
Out.println ("<BODY>");
Out.println ("<HR>");
Out.println ("<p>");
Out.println ("Reqdispacherservelt");
Out.println ("</P><p>");
Out.println ("ServerName:" +req.getservername () + "ServerPort:" +
Req.getserverport ());
Out.println ("</p>");
RequestDispatcher rd=req.getrequestdispatcher ("/admin");
RequestDispatcher rd2=req.getrequestdispatcher ("/control");
if (Type.equals ("REQUEST"))
{
}
if (Type.equals ("FORWARD"))
{
Rd.forward (REG,RESP);
}
if (Type.equals ("INCLUDE"))
{
Rd.include (REQ,RESP);
}
if (Type.equals ("ERROR"))
{
Resp.senderror (404, "Error from Reqdispacherservlet");
}
if (Type.equals ("Control"))
{
Rd2.forward (REQ,RESP);
}
Out.flush ();
}
Public Voiddestroy ()
{
System.out.println ("Reqdispacherservlet:destroy ()");
}
}

4. Enhanced internationalization function

Servlet 2.4 Adds to the internationalization of Web programs, where you can define the character encoding of a Web site in Web.xml.

<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>zh</locale>
<encoding>gb2312</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>

When a client requests a Web resource for a particular language, the Servlet program sets the language attribute of a Web response through the SetLocale method of the Servletresponse interface.

5, Login/logout function

The logout and login methods are added to the servlet 2.4 for ease of security management. You can refer to the Servlet 2.4 API to see its usage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.