Form encoding filter

Source: Internet
Author: User


If Spring is used, you can replace it with org. springframework. web. filter. CharacterEncodingFilter.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Filter>
<Filter-name> EncodingFilter </filter-name>
<Filter-class> org. springframework. web. filter. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> EncodingFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
</Web-app>


The following figure shows the web. xml configuration in the JavaWeb project.
[Html
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Filter>
<Filter-name> EncodingFilter </filter-name>
<Filter-class> com. jadyer. Filter. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> EncodingFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
</Web-app>

The following is the CharacterEncodingFilter. java filter used to set the form transmission parameter encoding.
[Java]
Package com. jadyer. Filter;
 
Import java. io. IOException;
 
Import javax. servlet. Filter;
Import javax. servlet. FilterChain;
Import javax. servlet. FilterConfig;
Import javax. servlet. ServletException;
Import javax. servlet. ServletRequest;
Import javax. servlet. ServletResponse;
 
/**
* Sets the encoding of transmission parameters.
*/
Public class CharacterEncodingFilter implements Filter {
Private String encoding = "ISO8859_1 ";
 
/**
* When the filter object is destroyed, the Web Container calls this method.
* @ See this method is mainly used to release the resources occupied by the filter object.
*/
Public void destroy (){}

/**
* The Web Container calls the filter object when the filter object is generated. It is mainly used to initialize the filter.
* @ See this method receives a FilterConfig-type parameter to obtain information about the current filter.
* @ See. For example, you can call the getFilterName () method of FilterConfig to obtain the name of the filter.
* @ See. For example, call the getServletContext () method to obtain the current ServletContext object.
* @ See here is mainly used to read the value of <param-name> encoding </param-name> set in web. xml.
*/
Public void init (FilterConfig config) throws ServletException {
Encoding = config. getInitParameter ("encoding ");
}
 
/**
* The doFilter () method is the core method in the Filter interface. It completes the Filter function.
* @ See FilterChain objects are objects that save the execution sequence of multiple filters.
* @ See a Web application can contain multiple filters. These filters are like beads being stringed together.
* @ See is called one by one. The call sequence is the order in which the filter is declared in web. xml.
* @ See: by calling the doFilter () method of the FilterChain object, you can call the doFilter () method of the next filter.
* @ See if the next filter is the last one, the Servlet, JSP, or other files requested by the client are called.
*/
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Request. setCharacterEncoding (encoding); // sets the form parameter encoding method.
Chain. doFilter (request, response );
}
}

Related Article

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.