Configuration and application of filter filters

Source: Internet
Author: User

This myfilter filter is used for encoding conversion, cache settings, Request Interception, and corresponding configuration. ) And replace it. Check whether the user exists in the current session. You can submit the user to the display. jsp page only when you enter the same user name twice in a row.

1. Compile the registration request page (regist. jsp). The Code is as follows:

<% @ Page contenttype = "text/html; charset = GBK "%> <br/> <HTML> <br/> <body> <br/> <center> <br/> <form action =" display. JSP "method =" Post "> <br/> <Table> <br/> <tr> <br/> <TD> User Name: </TD> <br/> <TD> <input type = "text" name = "name"> </TD> <br/> </tr> <br/> <tr> <br/> <TD> credential: </TD> <br/> <TD> <input type = "text" name = "title" size = "10"> </TD> <br/> </tr> <br/> <tr> <br/> <TD> description: </TD> <br/> <textarea name = "content" rows = "5" Cols = "20">

2. Compile the filter class (myfilter. Java)

Package COM. filter; </P> <p> Import Java. io. *; <br/> Import Java. util. *; <br/> Import javax. servlet. *; <br/> Import javax. servlet. HTTP. *; </P> <p> public class myfilter implements filter {<br/> private string encoding = ""; // encoding <br/> Private Static final string word_file = "word_file"; <br/> hashmap <string, string> Hm = new hashmap <string, string> (); </P> <p>/* in the init () method, read the file with the replaced content and save it to the hashmap object. */<Br/> Public void Init (filterconfig) throws servletexception {<br/> // obtain information about the configuration file <br/> string configpath = filterconfig. getinitparameter (word_file); <br/> // get the encoding <br/> encoding = filterconfig. getinitparameter ("encoding"); <br/> // obtain the context <br/> servletcontext SC = filterconfig. getservletcontext (); <br/> string filepath = SC. getrealpath (configpath); <br/> // read the configuration file <br/> try {<br/> filereader Fr = new filereader (filepath); <br/> bufferedreader BR = new bufferedreader (FR); <br/> string line; <br/> // put the corresponding key value in Map <br/> while (null! = (Line = BR. readline () {<br/> string [] strtemp = line. split ("="); <br/> Hm. put (strtemp [0], strtemp [1]); <br/>}< br/>} catch (ioexception IE) {<br/> throw new servletexception ("An error occurred while reading the file! "); <Br/>}</P> <p> Public void dofilter (servletrequest request, servletresponse response, <br/> filterchain chain) throws ioexception, servletexception {<br/> httpservletrequest httpreq = (httpservletrequest) request; <br/> httpservletresponse httpresp = (httpservletresponse) response; </P> <p> // obtain the packaging Class Object of the request and response object. <Br/> myrequestwrapper reqwrapper = new myrequestwrapper (httpreq); <br/> myresponsewrapper respwrapper = new myresponsewrapper (httpresp ); <br/> // filter the Request Encoding <br/> reqwrapper. setcharacterencoding (encoding); <br/> // verify whether the user exists <br/> httpsession = httpreq. getsession (); <br/> // you need to use the httpreq object to obtain parameters for judgment <br/> If (httpreq. getparameter ("name ")! = NULL <br/> &&""! = Httpreq. getparameter ("name "). trim () {<br/> // enter the user name in the session after setting. <br/> If (httpsession. getattribute ("username ")! = NULL) {<br/> If (! Httpsession. getattribute ("username "). equals (<br/> reqwrapper. getparameter ("name") {<br/> httpsession. setattribute ("username", reqwrapper <br/>. getparameter ("name"); <br/> // return to the registration page <br/> respwrapper. sendredirect ("regist. JSP "); <br/>}// enter the same user name for the second time and enter the display page <br/>} else {<br/> // return to the registration page <br/> httpsession. setattribute ("username", reqwrapper <br/>. getparameter ("name"); <br/> respwrapper. sendredirect ("R Egist. JSP "); <br/>}< br/>} else {<br/> // return to the registration page <br/> respwrapper. sendredirect ("regist. JSP "); <br/>}< br/> // set the browser to not cache the page <br/> respwrapper. setheader ("cache-control", "No-Cache"); <br/> respwrapper. setheader ("Pragma", "No-Cache"); // HTTP 1.0 <br/> respwrapper. setdateheader ("expires", 0); <br/> chain. dofilter (reqwrapper, respwrapper); <br/> string content = new string (respwrapper. tobytearray (); <br/> stri Ng result = replacetext (content); <br/> printwriter out = httpresp. getwriter (); <br/> out. println (result); <br/> out. close (); <br/>}</P> <p>/* filter indecent words */<br/> Public String replacetext (string content) throws ioexception {<br/> stringbuffer sb = new stringbuffer (content); <br/> set Keys = HM. keyset (); <br/> for (INT flag = 0; flag <1;) {<br/> iterator it = keys. iterator (); <br/> while (it. hasnext () {<br /> String key = (string) it. Next (); <br/> int Index = sb. indexof (key); <br/> If (-1! = Index) {<br/> Sb. replace (index, index + key. length (), (string) Hm <br/>. get (key); <br/>}< br/> If (iscontains (SB) {<br/> flag = 1; <br/>}else {<br/> flag = 0; <br/>}< br/> return sb. tostring (); <br/>}< br/> // determines whether the filtering is complete. <br/> Public Boolean iscontains (stringbuffer SB) {<br/> set Keys = HM. keyset (); <br/> iterator it = keys. iterator (); <br/> while (it. hasnext () {<br/> string key = (string) it. next (); <br/> If (sb. tostring (). contains (key) {<br/> return false; <br/>}< br/> return true; <br/>}</P> <p> Public void destroy () {<br/>}< br/>}

3. Compile the interception request packaging class (myrequestwrapper. Java)

Package COM. filter; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletrequestwrapper; </P> <p> Public final class myrequestwrapper extends httpservletrequestwrapper <br/>{< br/> Public myrequestwrapper (httpservletrequest request) <br/>{< br/> super (request); <br/>}< br/>/* rewrite the getparameter () method to filter the value of the request parameter. */<Br/> Public String getparameter (string name) <br/>{< br/> string value = super. getparameter (name); <br/> If (null! = Value) {<br/> return tohtml (value. trim (); <br/>}< br/> else <br/> return NULL; <br/>}< br/>/* converts special characters to corresponding entity references or character references. */<Br/> private string tohtml (string Str) <br/>{< br/> If (STR = NULL) <br/> return NULL; <br/> stringbuffer sb = new stringbuffer (); <br/> int Len = Str. length (); <br/> for (INT I = 0; I <Len; I ++) <br/> {<br/> char c = Str. charat (I); <br/> switch (c) <br/>{< br/> case '': <br/> Sb. append (""); <br/> break; <br/> case '/N': <br/> Sb. append ("<br>"); <br/> break; <br/> case '/R': <br/> break; <br/> case '/'': <br/> Sb. append ("'"); <br/> break; <br/> case' <': <br/> Sb. append ("<"); <br/> break; <br/> case '>': <br/> Sb. append (">"); <br/> break; <br/> case '&': <br/> Sb. append ("&"); <br/> break; <br/> case '"': <br/> Sb. append ("); <br/> break; <br/> case '//': <br/> Sb. append ("/"); <br/> break; <br/> default: <br/> Sb. append (c); <br/>}< br/> return sb. tostring (); <br/>}< br/>

4. Compile the corresponding packaging class (myresponsewrapper. Java)

Package COM. filter; </P> <p> Import Java. io. *; <br/> Import javax. servlet. *; <br/> Import javax. servlet. HTTP. *; </P> <p> public class myresponsewrapper extends httpservletresponsewrapper <br/>{< br/> private bytearrayoutputstream baos; <br/> private printwriter PW; </P> <p> Public myresponsewrapper (httpservletresponse response) <br/>{< br/> super (response ); <br/> // Replace the default output stream object <br/> baos = new bytearrayoutputs Tream (); <br/> PW = new printwriter (baos); <br/>}< br/> Public printwriter getwriter () <br/>{< br/> return PW; <br/>}< br/>/* return the content in the output stream buffer in the form of a byte array. */<Br/> Public byte [] tobytearray () <br/>{< br/> return baos. tobytearray (); <br/>}< br/>}

5. Display request information (display. jsp)

<% @ Page contenttype = "text/html; charset = GBK "%> <br/> <HTML> <br/> <pead> <br/> <title> display information page </title> <br/> </ head> <br/> <body> <br/> <a href = "regist. JSP "mce_href =" regist. JSP "> I want to submit information </a> <br/> <% <br/> system. out. println ("wwwwww"); <br/> out. println ("username:" + request. getparameter ("name"); <br/> out. println ("Identity:" + request. getparameter ("title"); <br/> out. println ("info:" + request. getparameter ("content"); <br/> out. flush (); <br/>%> <br/> </body> <br/> </ptml>

6.replace character file (word.txt) (key value)

My day = You * <br/> fuck = ***** <br/> fuck = him ** <br/> fuck = *

7. Web. xml file configuration

<? XML version = "1.0" encoding = "gb2312"?> </P> <p> <web-app xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" <br/> Version = "2.4"> <br/> <filter-Name> myfilter </filter-Name> <br/> <filter-class> COM. filter. myfilter </filter-class> <br/> <init-param> <br/> <param-Name> word_file </param-Name> <br/> <param-Value >/WEB-INF/word.txt </param-value> <br/> </init-param> <br/> <param-Name> Encoding </param-Name> <br/> <param-value> GBK </param-value> <br/> </init-param> <br/> </filter> <br/> <filter-mapping> <br/> <filter-Name> myfilter </filter-Name> <br/> <URL-pattern>/index. JSP </url-pattern> <br/> <dispatcher> request </dispatcher> <br/> <dispatcher> forward </dispatcher> <br/> </filter-Mapping> </P> <p> </Web-app>

) And replace it. Check whether the user exists in the current session. After you enter the same user name twice in a row, the submitted information is displayed on the display. jsp page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.