Introduction of Filter:
The filter can process the customer's request in advance and then forward the request to another Web component.
The filter is defined in the Servlet2.3 specification and can be checked and modified for Web Components, ServletRequest, and Servletresponse.
The filter itself does not generate ServletRequest objects and Servletresponse objects, it only provides the following filtering functionality for Web Components:
Check the request before the Web component is invoked, and modify the header and request body.
The filter can check the response image after the Web component is invoked, and modify the response header and response body.
Custom filters must be implemented: the Javax.servlet.Filter interface. This interface defines the following three methods:
Init (Filterconfig conf) – The initialization method of the filter. -Initialize the execution of the work once (at startup).
Dofilter (Servletrequest,servletresponse,filterchain) – This method completes the actual over action. This method is executed whenever the configured URL matches the configuration of this filter.
The Destroy ()-servlet window executes this method when the filter is destroyed. -Destruction is done only once.
Memory is not good, simple record a demo
Web.xml Configuration
<filter>
<filter-name>zfx</filter-name>
<filter-class>cn.zfx.filter.zfxfilter </filter-class>
<init-param>
<param-name>encoding</param-name>
< param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping >
<filter-name>zfx</filter-name>
<url-pattern>/*</url-pattern>
< dispatcher>request</dispatcher><!--default-->
</filter-mapping>
/**
* Filter
/Public
class Zfxfilter implements filter{
private String encoding;
@Override public
Void Destroy () {
System.out.println ("Hung ..."). ");
}
@Override public
void Dofilter (ServletRequest req, Servletresponse resp,
filterchain chain) throws IOException, servletexception {
System.out.println ("Filtered ..."). "+ encoding);
req.setcharacterencoding (encoding);
Chain.dofilter (req, resp);
System.out.println ("post-processing filtered ....") "+ encoding);
}
/**
* can read initialization information *
/@Override public
void init (filterconfig config) throws Servletexception {
this.encoding = config.getinitparameter ("encoding");
}
Filters can do a lot of things, such as: Character set filter, set JSP page does not cache, verify that users are logged in, verify illegal characters, which request.setcharacterencoding ("UTF-8"), can only solve the post method of garbled, can't solve get way. So here you can use the wrapper mode, packaging request, for request enhancements, Java IO is basically used in the packaging design mode
Packaging design mode:
For the enhancement of a class, generally take three kinds of type
1, inherit the enhanced class, that is, implement a subclass.
2, the use of dynamic agents to deal with the need for enhanced methods.
3, the use of packaging design mode. (Io in Java is basically a wrapper design pattern)
The following steps are used to enhance a class using the wrapper design pattern:
1, inheritance needs to enhance the class.
2, the declaration needs to enhance the class for its own member variables.
3. Write a construction method to receive the classes that need to be enhanced.
4, to achieve the need for enhanced methods.
5, other ways to achieve scalability.
The packaging design pattern means:
Assuming Class A is the wrapper class of Class B, then type A has the same interface as Class B, and Class A has an instance of Class B, and Class A uses an instance of Class B to implement the interface.
/**
* Custom HttpServletRequest * Implementation of the
Httpservletrequestwrapper class
* To implement packaging mode
* with a custom request to implement garbled solution
* includes get and post
* @author Wangjianme */public
class Myhttpservletrequest
extends httpservletrequestwrapper{
private httpservletrequest request;
Public myhttpservletrequest (HttpServletRequest request) {
super (request);
This.request = Request;
}
@Override public
String getparameter (string name) {
System.err.println ("Get parameters .... ");
String param = request.getparameter (name);
if (param==null) { //If Empty then return null directly
;
}
Note The case problem
if (Request.getmethod (). Equalsignorecase ("get")) {
System.err.println ("Make transcoding");
If get is the transcoding, remove the encoding mode
try {
param = new String (param.getbytes ("iso-8859-1") from request, Request.getcharacterencoding ());
} catch (Unsupportedencodingexception e) {
throw new RuntimeException (E.getmessage (), E);
}
return param;
} else{return
param
}}
/**
* Solve garbled Filter
* @author wangjianme * * * Public
class Characterfilter implements filter{
public void Destroy () {
} public
void Dofilter (ServletRequest request, servletresponse response,
Filterchain Chain) throws IOException, servletexception {
System.err.println ("Start worrying .... ");
Request.setcharacterencoding ("UTF-8");
Note Create your own request
myhttpservletrequest req = new Myhttpservletrequest ((httpservletrequest) request);
Notice one below, must pass own req to resemble, otherwise bad use
chain.dofilter (req,response);
The following is not the use of their own request, then processing get will inevitably appear garbled
//chain.dofilter (request, response);
}
public void init (Filterconfig filterconfig) throws servletexception {
}
}