Filter technology on Java servlet (transfer)

xiaoxiao2021-03-06  72

Filter technology on Java servlet (Add Date: 2002-5-3: 2834) [Comment on this article] [Program Lovers Forum] [Save article to hard disk] [Print article]

Author: Li Wenjun Filter technology is servlet 2.3 new functions .servlet2.3 is sun and company in October 2000 release, its developers, including many individuals and corporate bodies, fully embodies the principle of open Code sun advocated by the company Due to the common efforts of many participants, servlet2.3 is much better than previous functions, and performance has also increased significantly. Its new features include: 1. Application lifecycle event control; 2. New Internationalization ; 3. Clarify the load rules of the class; 4. New error and security attributes; 5. Do not agree with the HTTPUTILS class; 6. Various useful methods; 7. Explain and extend several servlet DTDs; 8. Filter function The most important thing is Filter function. It enables users to change a request and modify a response. Filter is not a servlet, it can't generate a response, it can prepare the request before a request to servlet, or leave the servlet Handling response. Swelling, Filter is actually a "servlet chain). A Filter includes: 1. Intercept before servlet is called; 2. Check Servlet Request; 3. Modify Request as needed. Head and Request data; 4. Modify response header and response data as needed; 5. Intercept after servlet is called. You can configure a filter to one or more servlets; a single servlet or servlet group can be used by multiple Filter. A practical FILTER includes: user identifying the file, the log filter, the audit filter, the encryption filter, the symbol filter, can change the XSLT Filter of the XML content. A Filter must implement the javax.servlet.filter interface and define three methods: 1.void SetFilterConfig (FilterConfig Config) // Sets the Filter's configuration object; 2. FilterConfig getfilterConfig () // Returns the configuration object of the Filter; 3. Void Dofilter (ServletRequest Req, ServletResponse Res, Filterchai N chain) // Execute the Filter's work. The server only calls the setFilterConfig method once again, and the DOFILTER method is called multiple times to handle different requests. The FilterConfig interface can find the Filter name and initialization parameter information. Server can set FilterConfig is empty to indicate that Filter has ended. Each Filter gets current Request and Response in the DOFILTER () method. In this method, any operation for Request and Response can be performed. (Including collecting data, packaging data, etc.) .filter calls chain.dofilter () method to hand over the control right to the next filter. One Filter ends in the DOFILTER () method. If a Filter wants to stop the Request processing, get complete control of Response, then it can not call A Filter. A FILTER can package Request or Response to change several methods and provide user-customized properties. API2.3 provides HTTPSERVLETREQUESTWrapper and HttpServletResponseWrapper to achieve. They can assign initial request and response. If you want to change a method, if you want to change a method, and an override method must inherit wapper Here is a simple log filter is used to record the duration of all request public class LogFilter implements filter {FilterConfig config;.. public void setFilterConfig (FilterConfig config) {this.config = config;

转载请注明原文地址:https://www.9cbs.com/read-88576.html

New Post(0)