Java Web Review 11th filters (with Chinese garbled characters) and Java Web 11th

Source: Internet
Author: User

Java Web Review 11th filters (with Chinese garbled characters) and Java Web 11th
1: filter concept

A filter is an intermediate component that requests the target resource. A sewage purification device is needed to convert the sewage into pure water, which is like a filter. Then I use graphs to indicate the process of running the filter (multiple filters can be included ).

2: Filter Interface

The Filter Interface contains three methods: init, doFilter, and destroy.

2.1: public void init (FilterConfig filterConfig)

This method is mainly used to initialize the filter. When the container calls this method, it transmits the FilterConfig object to the filter. The usage of FilterConfig is similar to that of ServletConfig. The FilterConfig object can be used to obtain the ServletContext object, and get the parameters initialized by the filter in the deployment descriptor.

2.2: public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain)

This method is similar to the service in Servlet. When the client requests the target resource, the container will call the doFilter method of the filter associated with the target resource, in this method, you can process requests and responses to implement specific filter functions and call chain after processing is complete. doFilter sends the request to the next filter (target resource)

2.3: public void destroy ()

When the filter lifecycle ends, the container will call this method to release the filter resources.

3: filter configuration

Filters are implemented through the <filter> and <filter-mapping> elements.

3.1: <filter> element

<Filter> attribute of the element. <filter-name> is used to specify a name for the filter. The <filter-class> element is used to specify the complete qualified class name of the filter, <init-param> is used to specify initialization parameters for the filter. It has two attributes: <param-name> parameter name and <param-value> parameter value.

3.2: <filter-mapping> element

<Filter-mapping> there are three main attributes below. <filter-name> is used to specify the name of the filter that has been declared in the container, either <url-pattren> or <servlet-name>. <url-pattren> indicates that the filter is called when such a url is requested, <servlet-name> indicates that the filter is called when the Servlet is called (multiple servlets may exist ).

Now that the basic knowledge is ready, let's start working together.

4: filter time application 4.1: Filter solves Chinese garbled characters

Since the Web Container's default encoding method is ISO-8859-1, in Servlet/Jsp programs, the request object's getParameter () the method to get the string is converted to the ISO-8859-1 (someone said I have set the UTF-8 In the jsp program why is this still happening, it is because the web Container once again on the request data conversion, so then get the string is the ISO-8859-1 encoding), this is the cause of garbled. For example

After finding the root cause, the problem is better solved. The core code is as follows:

Public class EncodingFilter implements Filter {protected String encoding = null; class myRequestWrapper extends {public myRequestWrapper (HttpServletRequest request) {super (request);} public String getParameter (String param) {String value = null; try {// post submit super. setCharacterEncoding (encoding); value = super. getParameter (param); if (super. getMethod (). equalsIgnoreCase ("GET ")) // Get submit {if (value! = Null) {value = new String (value. getBytes ("iso8859-1"), encoding) ;}} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return value;} public void init (FilterConfig filterConfig) throws ServletException {this. encoding = filterConfig. getInitParameter ("encoding"); // obtain the filter initialization parameter from the web Container} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {// response to avoid garbled response. setCharacterEncoding ("text/html; charset =" + this. encoding); // give the filter to the next filter or resource processor chain. doFilter (new myRequestWrapper (HttpServletRequest) request), response);} public void destroy () {this. encoding = null ;}}

Configure the filter

<filter>        <filter-name>encoding</filter-name>        <filter-class>com.lp.filters.EncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encoding</filter-name>        <servlet-name>LoginServlet</servlet-name>        <!-- <url-pattern>/LoginServlet</url-pattern> -->    </filter-mapping>

OK, the above filter is complete. You can test it on your own.

 

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.