A servlet filter landing
Abstract: A servlet filter landing
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="416" height="86" align="left" valign="top"> 1.Servlet filters?
Servlet filters it is a small component of the Web, they intercept the request and response to see, in some way or extraction operation is between clients and servers exchange information. Packaging is usually filter function of a number of Web components, although these functions are important, but to deal with the client request or send response is not decisive. Typical examples include the record on the request and response data, security agreements, management session attributes, and so on. Filters provide a modular object-oriented mechanism for public tasks to be inserted into packaging components, and these components through a configuration file to the statement, and dynamic handling.
Servlet filters in combination with a lot of elements, so that filters a unique, powerful and modular Web components. In other words, Servlet filters is:
- Statement: filter through the Web deployment descriptor (web.xml) XML tags to the statement. This allows to add and delete filter, without the need to change any application code or JSP pages.
Dynamic: filter when in operation by the Servlet containers called to intercept and deal with the request and response. </ Td> <td width="268" valign="top"> </ td> </ tr> </ table>
Flexible: Web filters in the environment of very broad, covering such as logging and security, many of the public complementary role. Filters or flexible, because they can be used to directly from the client call the implementation of pretreatment and post-processing, as well as in the firewall between the components of Web scheduling request. Finally, the filters can be chained together to provide the required functionality. LogonStrings Login.jsp IncludeStrings . Jsp;. Html RedirectPath / Login.jsp Disabletestfilter N
Modular: The logic of the application package to deal with a single type of document, which defines the filter can be easily from the request / response chain add or remove modular unit.
Portability: the Java platform and many other areas, Servlet filters are cross-platform and cross-containers can be transplanted, thus further supporting the Servler filters and reusable modular nature.
Reusable: attributed to the realization of the filter modular design, as well as a statement of the filter configuration, the filter can be easily across different projects and applications to use.
Transparent: in the request / response chain, including filters that are designed to supplement (but not in any way substitute) servlet or JSP page with the core processing. As a result, filters can be added or removed as needed, and would not undermine the servlet or JSP pages.
2.Servlet filter architecture
As its name suggests, Servlet filters used for intercepting incoming request and / or to come. Response, and to monitor, modify or in some way is through the handling of the data stream. Filter is self-contained, modular components, they can be added to the request / response chain, or in the application without affecting the other components of the Web delete them. Filter is only the request and respond to changes in the handling of the operation, thus they should not be directly embedded Web application framework, unless it is through Servlet API in well-defined standard interfaces to achieve.
Web resources can be configured to have no correlation with filters (which is the default), associated with a single filter (This is a typical situation), and even with a filter chain linked. So what filters do? Like servlet, it accepted the request and response objects. Then filter checks the request object, and decided to forward the request to the next in the chain components, or suspension of the request directly to the client and sent back a response. If the request is forwarded, it will be passed on to the next one in the chain of resources (other filters, servlet or JSP page). In this request to filter through the processing chain by the server, a response will be the reverse order sent back through the chain. This gave each of the filters are provided in accordance with the need to address targeted response to the opportunities.
When the filters in the Servlet 2.3 specification was first introduced, they can filter Web clients and client visits by the designated between the content of Web resources. If the request scheduling resources and then to other Web resources, it can not be entrusted to any behind-the-scenes request application filters. 2.4 norms eliminate this restriction. Servlet filters can now be applied to J2EE Web environment in the request and response object anywhere. Therefore, Servlet filters can be applied to clients and servlet between servlet or servlet and JSP pages, as well as between each covered by the JSP pages between. This is what I would call a strong capacity and flexibility!
3. Prepared to achieve such procedures
Filter API contains three simple interface, clean and they nested in the javax.servlet package. The three interfaces are Filter, and FilterChain FilterConfig. From the programming point of view, filter categories will achieve Filter interface, and then use this type of filter and FilterChain FilterConfig interface. The filter will be used like a FilterChain object to the transfer, to allow the filters to transfer control of a chain of resources. FilterConfig object containers will be provided to the filter to allow access to the filter initialization data.
To our three-step model line, filters must use three methods in order to fully realize Filter Interface:
Init (): examples of this approach in containers of the filter is called, is designed to make it the main filters to deal with the preparation. The method accept a FilterConfig type of object as an input.
DoFilter (): servlet has a service () method (this method also calls doPost () or doGet ()) to deal with the request, the filter has to deal with individual requests and responses? D? D doFilter (). This method accept three input parameters: a ServletRequest, and a response object FilterChain.
Destroy (): As you imagine, as this method to perform any clean-up operation, these operators may be required in the automated refuse collection before.
SessionFilter.java
Package net.pms.web.filter;
Import java.io.IOException;
Import javax.servlet.Filter;
Import javax.servlet.FilterChain;
Import javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import javax.servlet.ServletRequest;
Import javax.servlet.ServletResponse;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpServletResponseWrapper;
/ **
* @ Author jfish
* @ Since 2006.1.12
* /
Public class SessionFilter implements Filter (
Public static boolean isContains (String container, regx String []) (
Boolean result = false;
For (int i = 0; i <regx.length; i + +) (
If (container.indexOf (regx [i])! = -1) (
Return true;
)
)
Return result;
)
Public FilterConfig config;
Public void setFilterConfig (FilterConfig config) (
This.config = config;
)
Public FilterConfig getFilterConfig () (
Return config;
)
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException (
HttpServletRequest httpreq = (HttpServletRequest) request;
HttpServletResponse httpres = (HttpServletResponse) response;
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper (
(HttpServletResponse) response);
String logonStrings = config.getInitParameter ( "logonStrings");
String includeStrings = config.getInitParameter ( "includeStrings");
String redirectPath httpreq.getContextPath = ()
+ Config.getInitParameter ( "redirectPath");
String disabletestfilter = config.getInitParameter ( "disabletestfilter");
If (disabletestfilter.toUpperCase (). Equals ( "Y")) (
Chain.doFilter (request, response);
Return;
)
String [] = logonStrings.split (";"); logonList
String [] = includeStrings.split (";"); includeList
Object user = httpreq.getSession (). GetAttribute ( "userinfo");
If (user == null) (
If (! This.isContains (httpreq.getRequestURI (), includeList)) (
Chain.doFilter (request, response);
Return;
)
If (this.isContains (httpreq.getRequestURI (), logonList)) (
Chain.doFilter (request, response);
Return;
)
Wrapper.sendRedirect (redirectPath);
Else ()
Chain.doFilter (request, response);
)
)
Public void destroy () (
This.config = null;
)
Public void init (FilterConfig filterConfig) throws ServletException (
This.config = filterConfig;
)
)
4. Configuration Servlet filters
In web.xml:
These parameters logonStrings, landing pages
IncludeStrings, filter parameters pages
RedirectPath, not to landing pages
Disabletestfilter, filters effectively.
</ Td> </ tr> <tr>
↑ Back
Tags: java filter






