Understanding of Chain.dofilter (Request,response)

Source: Internet
Author: User

His role is to forward the request to the next object on the filter chain. What's the "down" point here?

refers to the next filter, if there is no filter that is the resource you requested.

General filter is a chain, Web. XML is configured with several. and one by one.

Request resource, Filter2->filter3---Filter1

Here is an example: input.jsp is used to submit input, when submitted, the filter detects the name and age, if the entire regular will be submitted to output.jsp, if not normal to erroroutput.jsp. There is also a filter, It is used to detect whether a page has character encoding set, if not. (prevents garbled problems). ==================Encodefilter.java===================package Servletbean;public class Encodefilter implements Filter {public void DoFilter ( ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {if ( Request.getcharacterencoding () ==null) {System.out.println (encoding); request.setcharacterencoding (encoding);} Chain.dofilter (request, response);//To the next chain}public void Init (Filterconfig fconfig) throws Servletexception {This.config =fconfig;encoding=fconfig.getinitparameter ("Encoding");} }==============input.jsp==============<form action= "output.jsp" name= "form" method= "POST" ><table><tr><td>name< /td><td><input type= "text" name= "name"/></td></tr><tr><td>age</td> <td><input type= "text" name= "age"/></td></tr><tr><td><input type= "Submit" Name= "OK" value= "OK"/></td></tr></table></form>Myfilter.java======================package Servletbean;import Javax.swing.joptionpane; public class Myfilter implements Filter {public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Ser Vletexception {   response.setcontenttype ("text/html");  response.setcharacterencoding ("GB2312");   PrintWriter out=response.getwriter ();  string Name= "";  string age= "";  int age1;  name= Request.getparameter ("name");  Age=request.getparameter ("Age");  RequestDispatcher dispatch= Request.getrequestdispatcher ("erroroutput.jsp");    if (name==null| | name== "" | | name== "" | | Age==null) {Joptionpane.showmessagedialog (null, "User name and age input Error!) ");d Ispatch.forward (request, response); return;} Else{try{age1=integer.parseint (age);} catch (Exception e) {//joptionpane.showmessagedialog (null, "age must be a number! ");d Ispatch.forward (request,response); return;//If the error page is in erroroutput.jsp}}Chain.dofilter (Request, response);//here is said to be correct, that is, he went back to find the next chain, but it has no, so it will go to jump page, at this time to jump to the page is action= "output.jsp". }}================Web. XML=============== Note: Filter is in order <filter><description></description><display-name> Encodefilter</display-name><filter-name>encodefilter</filter-name><filter-class>   Servletbean.encodefilter</filter-class><init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value></init-param></filter><filter-mapping>< Filter-name>encodefilter</filter-name><url-pattern>/*</url-pattern></filter-mapping ><filter><description></description><display-name>myfilter</display-name>< filter-name>myfilter</filter-name><filter-class>servletbean.myfilter</filter-class></ filter><filter-mapping><filter-name>myfilter</filter-name><url-pattern>/output.jsp </url-pattern></filter-mapping>

Understanding of Chain.dofilter (Request,response)

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.