1. Chinese garbled filter. Get and set Chinese font settings
Public class allcharacterencodingfilter implements filter {private filterconfig; Public void Init (filterconfig) throws servletexception {This. filterconfig = filterconfig;} public void dofilter (servletrequest req, servletresponse resp, filterchain chain) throws ioexception, servletexception {httpservletrequest request = (httpservletrequest) req; Response response = (response) resp; // obtain the value of the configured filter parameter encoding: If no value exists, the default value is string encoding = filterconfig. getinitparameter ("encoding"); If (encoding = NULL) encoding = "UTF-8"; // sets the encoding of the Request parameters for the post request method. setcharacterencoding (encoding); // sets the encoding for the response output: Response stream and word throttling response. setcharacterencoding (encoding); // notify the client of the decoding code list response when the byte stream is output. setcontenttype ("text/html; charset =" + encoding); // the decoder code table of the notification client when the byte stream is output; Response stream: the code table used for the stream output and the code table used for notifying the client to decode myhttpservletrequest mrequest = new myhttpservletrequest (request); // release the chain. dofilter (mrequest, response);} public void destroy () {}}// use the packaging class design mode // The code inside httpservletrequestwrapper is the same as the default adapter in the connection pool principle. Class myhttpservletrequest extends httpservletrequestwrapper {public myhttpservletrequest (httpservletrequest request) {super (request) ;}// only Public String getparameter (string name) {string value = super. getparameter (name); If (value = NULL) return value; // obtain the Request Method string method = super. getmethod (); If ("get ". repeated signorecase (method) {try {value = new string (value. getbytes ("ISO-8859-1"), super. getcharacterencoding ();} catch (unsupportedencodingexception e) {e. printstacktrace () ;}} return value ;}}
2. Dirty Filter
Public class implements filter {public void destroy () {} public void dofilter (servletrequest req, incluresp, filterchain chain) throws ioexception, servletexception {httpservletrequest request = (httpservletrequest) req; httpservletresponse response = (httpservletresponse) resp; dirtywordshttpservletrequest drequest = new dirtywordshttpservletrequest (request); chain. dofilter (drequest, response);} public void Init (filterconfig) throws servletexception {}} class extends httpservletrequestwrapper {private string words [] = {"silly B", "animal ", "animal"}; Public dirtywordshttpservletrequest (httpservletrequest request) {super (request) ;}@ overridepublic string getparameter (string name) {string value = super. getparameter (name); If (value = NULL) Return ""; for (string DWORD: words) {If (value. contains (DWORD) {// determines whether the content entered by the user contains the swearing value = value. replace (DWORD, "**") ;}} return value ;}}
3. html special mark Filter
// Html tag filter: <public class htmlfilter implements filter {public void destroy () {} public void dofilter (servletrequest req, servletresponse resp, filterchain chain) throws ioexception, servletexception {httpservletrequest request = (httpservletrequest) req; httpservletresponse response = (httpservletresponse) resp; htmlhttpservletrequest hrequest = new htmlhttpservletrequest (request); chain. dofilter (hrequest, response);} public void Init (filterconfig) throws servletexception {}} class htmlhttpservletrequest extends {public htmlhttpservletrequest (httpservletrequest request) {super (request );} @ overridepublic string getparameter (string name) {string value = super. getparameter (name); If (value = NULL) return value; value = htmlfilter (value); // complete tag filtering return value;} private string htmlfilter (string message) {If (Message = NULL) Return (null); char content [] = new char [message. length ()]; message. getchars (0, message. length (), content, 0); stringbuffer result = new stringbuffer (content. length + 50); For (INT I = 0; I <content. length; I ++) {Switch (content [I]) {Case '<': result. append ("<"); break; Case '>': result. append (">"); break; Case '&': result. append ("&"); break; Case '"': result. append ("); break; default: result. append (content [I]) ;}} return (result. tostring ());}}
Configuration in Web. xml
<filter><filter-name>AllCharacterEncodingFilter</filter-name><filter-class>cn.itcast.filter.AllCharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param></filter><filter-mapping><filter-name>AllCharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>DirtyWordsFilter</filter-name><filter-class>cn.itcast.filter.DirtyWordsFilter</filter-class></filter><filter-mapping><filter-name>DirtyWordsFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>HtmlFilter</filter-name><filter-class>cn.itcast.filter.HtmlFilter</filter-class></filter><filter-mapping><filter-name>HtmlFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter>