in the process of learning JSP and Java, there are always garbled cases. So we have to add this kind of information often:
<%@ page language= "java" contenttype= "text/html; charset=gb18030 " pageencoding=" GB18030 "%>
The filter is added later. Let's look at a picture:
This is the application that joins the filter in the middle of the Web client, server, and JSP pages.
Filter life cycle:
1 , load when starting server filter Span lang= "en-US" style= "Font-family:calibri" >init () method to initialize the instance ( filter servlet
2 , only methods are called on each request DoFilter () for processing
3 , stopping the server when calling Destroy () method, destroying the instance; (destroying an instance later than Servlet )
Let's take a look at the filter code for the background wrapper:
Package Com.bjpowernode.drp.util.filter;import java.io.ioexception;//These jar packages are some of the features of filter. Import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import javax.servlet.servletresponse;/** * Use the uniform processing character set * @author XZP * */public class Charsetencodingfilter implements Filter {private String endcoding; @Overridepublic void Destroy () {//TODO auto-generated method stub} @Overridepublic void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {System.out.println ("Charsetencodingfilter---> >>begin ");//Set CharSet request.setcharacterencoding (endcoding);//Continue to execute Chain.dofilter (request,response); System.out.println ("Charsetencodingfilter--->>>end");} @Overridepublic void init (Filterconfig filterconfig) throws Servletexception {this.endcoding= Filterconfig.getinitparameter ("Encoding");}}
Finally, XML configuration is required:
<?xml version= "1.0" encoding= "UTF-8"? ><filter><filter-name>charsetencodingfilter</ Filter-name><filter-class>com.bjpowernode.drp.util.filter.charsetencodingfilter</filter-class> <init-param><param-name>encoding</param-name><param-value>gbk</param-value></ init-param><filter-mapping> <filter-name>CharsetEncodingFilter</filter-name> < Url-pattern>*.jsp</url-pattern> </filter-mapping></filter>
Summarize:
1.Filter can be seen as a crosscutting technique.
2.Filter is embodied in the design pattern: "Responsibility chain Mode", "Template method Mode".
3.Filter provides a declarative service that intercepts request and response requests. And only works on the POST request.
Java Learning Filter