JSP recodes the data sent by the browser.

Source: Internet
Author: User

In a recent DRP project, garbled characters often occur when JSP is used to operate Chinese characters. These problems cause Chinese characters to fail to be entered or displayed properly. The settings of character sets and encoding methods of character sets are involved here.

The following code can be set in JSP/Servlet: pageEncoding = "GB18030", contentType = "text/html; charset = GB18030", request. setCharacterEncoding ("GB18030") and response. setCharacterEncoding ("GB18030"), the first two can only be used in JSP, and the last two can be used in JSP and Servlet.

Here, we will only talk about the encoding method when reencoding the data sent by the browser. As we all know, to recode the data sent by the browser, you only need one statement, which is very simple.

Solution 1: (simple)
Copy codeThe Code is as follows:
<Span style = "font-family: Microsoft YaHei; font-size: 18px"> request. setCharacterEncoding ("GB18030"); </span> <span style = "font-family: 'Microsoft yahei'; font-size: 18px"> </span>

However, there is a problem here. There are many character-level pages that need to be set. In addition, this method also lacks flexibility and imposes great restrictions on future maintenance. Therefore, I Optimized Solution 1 and added the Filter interface to abstract the statements that set character sets into a java class. This java class implements the Filter interface. Let's take a look at the code.

Solution 2: (Use Filter to process character sets in a unified manner)
Copy codeThe Code is as follows:
<Span style = "font-family: Microsoft YaHei; font-size: 18px"> 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 org. omg. CORBA. Request;

/**
* Unified processing of character sets using Filters
* @ Author jerry
*
*/
Public class CharsetEncodingFilter implements Filter {

Private String encoding = null;

Public void destroy (){

}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

// System. out. println ("CharsetEncodingFilter --- >>> begin ");
// Set the character set
Request. setCharacterEncoding (encoding );

// Continue execution
Chain. doFilter (request, response );

// System. out. println ("CharsetEncodingFilter --- >>> end ");
}

Public void init (FilterConfig filterConfig) throws ServletException {
This. encoding = filterConfig. getInitParameter ("encoding ");
// System. out. println ("System. out. println ---- >>> encoding" + encoding );
}
} </Span>

If the Filter class does not work, you need to configure it in web. xml.
Copy codeThe Code is as follows:
<Span style = "font-family: Microsoft YaHei; font-size: 18px"> <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> GB18030 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> CharsetEncodingFilter </filter-name>
<Url-pattern> *. jsp </url-pattern>
</Filter-mapping> </span>

Here, the encoding method is flexibly set and can be changed in the configuration file, which simplifies future maintenance.

In this small example, we can see that there are actually a lot of code that can be optimized, from simple code that can implement functions to better code that is not afraid of various modifications and maintenance, I think more about code optimization. Obviously, I am not good enough, more exercises and thoughts are needed.

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.