The use of Java in the Filter filter

Filters
filter is in between a client and a server resource file filters before accessing the resource file, make changes, judgment on the request through a series of filters, the request does not comply with the rules in the middle of blocked or modified . You may also be filtered response, block or modify the response.
Below, the request sent by the browser to submit to a filter for filtering the first, the release conform to the rules, filtered submitted to the next filter in the filter chain. Order filter and its related configuration in web.xml sequential chain, is arranged at the front of the front chain. When requested by the all filter chain resource file after the access, if not pass, it may be disposed in the middle of a filter.
Filters typically used for login authentication authority, resource access control, sensitive words filtered, character encoding conversion, etc. operations, to facilitate code reuse, the servlet also not necessary that each perform a corresponding operation.
Simple application of filters:
1, a new class, an interface implemented Filter (Note: is the javax.servlet Filter).
2, doFilter (request, response, chain) method override the filter. Further two init (), destroy () method generally does not require rewriting. DoFilter filtering operation process.
3, the filter disposed in web.xml. Here we must keep in mind a principle: in web.xml listener> Filters> servlet. That listener in web.xml configuration before filter, filter configuration before the servlet, or an error occurs.
<filter>
<filter-name> LoginFilter </ filter-name> // Filter Name
<filter-class> package path com.nnngu.filter.loginFilter </ filter-class> // filter class
<init-param> // Optional
<param-name> parameter names </ param-name> // filter initialization parameters
Parameter Value <param-value> </ param-value>
</ PAMM-the init>
</ filter >
<filter-mapping> // filter mapping
<filter-name> LoginFilter </ filter-name>
<URL-pattern> action specified filter range </ URL-pattern>
</ filter-mapping>
<URL-pattren > define the scope of the filter function. The following general rules:
1, and the role of all web resources: <URL-pattern> / </ URL-pattern>. The client requests go through the filter when the filter access any resource files, you can access through, or can not access.
2, applied to a folder and all files: <URL-pattern> / the dir /
</ url-pattern>
. 3, a particular type of document effect: <URL-pattern> . Extension </ url-pattern >. For example <URL-pattern> .jsp </ URL-pattern> jsp filter all requests for access to documents.
4, acting on AxiTrader rebate www.fx61.
If a filter requires a variety of file filters, it may be a plurality of <filter-mapping>, define a mapping url-pattern to define a filtering rule. As follows:
<filter>
<filter-name> LoginFilter </ filter-name>
<filter-class> com.nnngu.filter.loginFilter </ filter-class>
</ filter>
<filter-Mapping>
<filter-name> LoginFilter </ filter-name>
<URL-pattern>
.jsp </ URL-pattern>
</ filter-Mapping>
<Mapping-filter>
<filter-name> LoginFilter </ filter-name>
<URL-pattern> *. do </ URL-pattern>
</ filter-Mapping>
Example 1: login authentication filter implemented, no login access request is rejected and redirected to a login page.
void doFilter public (ServletRequest arg0, arg1 ServletResponse,
FilterChain arg2) throws IOException,

Response = the HttpServletResponse (the HttpServletResponse) arg1;
the HttpSession Request.getSession the session = ();
String path Request.getRequestURI = ();
Integer UID = (Integer) session.getAttribute ( "the userid");
IF (path.indexOf ( "/ Login .jsp ")> - 1) { // login page does not filter
arg2.doFilter (arg0, arg1); // submitted to the next filter
return;
}
IF (path.indexOf (" / register.jsp ")> - 1) {// Do not filter registration page
arg2.doFilter (Request, Response);
return;
}
! IF (UID = null) {// already logged
arg2.doFilter (request, response); // release, submitted to the next filter
} the else {
Response.sendRedirect ( "the login.jsp");
}
}
Example 2: set character encoding
public void doFilter (ServletRequest request, ServletResponse response,
Catena alberghiera the FilterChain) throws IOException, {ServletException
the HttpServletRequest request2 = (the HttpServletRequest) Request;
the HttpServletResponse RESPONSE2 = (the HttpServletResponse) Response;
request2.setCharacterEncoding ( "UTF-. 8");
response2.setCharacterEncoding ( "UTF-. 8");
the chain.doFilter ( Request, Response);
}
Create Filter
to create and implement a class Filter
Note:
      introducing packet is a packet in javax.servlet.Filter
      initialized init is initialized when the restart tomcat
      doFilter chain.doFilter method of release of
recommendations the doFilter the request and response is converted into belt protocol HttpServletRequset and HttpServletResponse
subclasses as Http band protocol is not with the protocol, which method is more abundant than the parent class, easy call latter method
the following creating an instance of the code Filter the
package cn ._02FilterChain;
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.annotation.WebFilter;br/>@WebFilter("/chain")
public class AFilter implements Filter{
br/>@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
br/>}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse) response;
System.out.println("A放行之前+++");
chain.doFilter(req, resp);
}
@Override
br /> System.out.println ( "release after A ---");
}
@Override
public void the destroy () {
// the TODO Auto-Generated Method Stub br />}
}
the Filter again in web.xml configuration
configuration methods
for re web.xml configuration 1.
2. the class and then add @WebFilter ( "knockdown path");
filter implementation of a chain
       is called in accordance with <filter-mapping> configured to call the
the following are examples of the code web.xml
<filter>
<filter-name> CHaINA </ filter-name>
<filter-class> cn._02FilterChain.AFilter </ filter-class>
</ filter>
<filter>
<filter-name > chainB </ filter-name>
<filter-class> cn._02FilterChain.BFilter </ filter-class>
</ filter>
<filter>
<filter-name>chainC</filter-name>
<filter-class>cn._02FilterChain.CFilter</filter-class>
</filter>
<!-- 执行顺序按照filter-mapping执行 -->
<filter-mapping>
<filter-name>chainB</filter-name>
<url-pattern>/chain</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>chainA</filter-name>
<url-pattern>/chain</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>chainC</filter-name>
<url-pattern>/chain</url-pattern>
</filter-mapping>
package com.zzxtit.common.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
the javax.servlet.ServletRequest Import;
Import javax.servlet.ServletResponse;
public class CharacterEncodingFilter the implements the Filter {
Private static String encoding = "UTF-. 8";
public void the init (the FilterConfig FilterConfig) throws ServletException {
encoding = filterConfig.getInitParameter ( "encoding" ?) == null "UTF-. 8": filterConfig.getInitParameter ( "encoding");
}
public void the doFilter (the ServletRequest Request, the ServletResponse Response, the FilterChain catena alberghiera)
throws IOException, ServletException {
Request.setCharacterEncoding (encoding);
}
}
Note:
(1) use getInitPamater is to obtain the default value of variables enciding from the web.xml
(2) the role of chain: general filter is a chain, web.xml is well equipped with several there are several. One by one together, chain.
代码实现
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.zzxtit.common.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Guess you like

Origin blog.51cto.com/14511863/2446199