How to set the encoding format for the filter in JSP development: filter encoding format

Source: Internet
Author: User

How to set the encoding format for the filter in JSP development: filter encoding format

How to Set encoding format for filter in JSP development

We know that in order to avoid garbled data submission, we need to set the encoding format before each request. After you copy and paste the request. setCharacterEncoding ("gb2312") for countless times, do you want to do this once and for all? Can I modify the encoding of all requests at one time? Use Filter. Its name is Filter,

The Code is as follows:

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; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class SetCharacterEncodingFilter implements Filter {// implement Fil Ter interface // stores the encoding format information private String encode = null; public void destroy () {} public void doFilter (ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, servletException {// convert HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp;/** judge on the web. whether the encoding format information is configured in the xml file * if it is null, set the encoding format to the encoding format in the configuration file * otherwise set the encoding format to GBK */if (this. encode! = Null &&! This. encode. equals ("") {request. setCharacterEncoding (this. encode); response. setCharacterEncoding (this. encode);} else {request. setCharacterEncoding ("GBK"); response. setCharacterEncoding ("GBK");}/** use the doFilter method to call the next filter or target resource (servlet or JSP page) in the chain ). * Chain. doFilter processes the remaining part of the filter (if any) and finally processes the requested servlet or JSP page. */Chain. doFilter (request, response);} public void init (FilterConfig config) throws ServletException {// get in web. the encoding format information configured in the xml file. this. encode = config. getInitParameter ("encode ");}}

The configuration information in the web. xml file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns =" http://java.sun.com/xml/ns/j2ee "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Version =" 2.4 "xsi: schemaLocation =" http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee /Web-app_2_4.xsd "> <! -- Filter configuration information --> <filter-name> SetCharacterEncodingFilter </filter-name> <! -- Filter sets the encoding file --> <filter-class> ssh. dlc. chp1.filter. SetCharacterEncodingFilter </filter-class> <init-param> <! -- Init-param defines the initialization parameters of the filter --> <description> set the name and encoding type for the parameters and values </description> <param-name> encode </param-name> <param-value> GBK </param-value> </init-param> </filter> <filter-mapping> <! -- Filter-mapping tells the container that all requests matching the pattern should be allowed through the access control filter. All. all accesses ending with do are first filtered through the filter file --> <filter-name> SetCharacterEncodingFilter </filter-name> <url-pattern> *. do </url-pattern> </filter-mapping> </web-app>

The above is the method used to set the encoding format for the filter. If you have any questions, please leave a message or go to the community on this site to discuss and discuss the progress. Thank you for reading this article and hope to help you, thank you for your support!

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.