JSP Chinese Character Processing code

Source: Internet
Author: User

There are a basket of online processing methods. The following describes two effective solutions I have used.
1. Compile a string processing function for the program, save it with a static file, and include it in the JSP page for processing Chinese characters,
Copy codeThe Code is as follows:
<%!
Public String codeToString (String str)
{
String s = str;
Try
{
Byte temp [] = s. getBytes ("ISO-8859-1 ");
S = new String (temp );
Return s;
}
Catch (Exception e)
{
Return s;
}
}
%>

To convert data, you only need to: coedeToString (request. getParameter.
2. A common method, with a filter, can be used once and for all.
Copy codeThe Code is as follows:
Package filters;
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. HttpServletRequestWrapper;
Public class TomcatFormFilter implements Filter {
/**
* Request. java
* Expand HttpServletRequestWrapper without affecting the original functions and providing
Some HttpServletRequest
* Functions in the interface. It can solve Chinese problems in Tomcat's default settings.
You need to replace
* Request object.
*/
Class Request extends HttpServletRequestWrapper
{
Public Request (HttpServletRequest request ){
Super (request );
}
/**
* Convert the internal code of the data read by the form.
* Convert the ISO character to GBK.
*/
Public String toChi (String input ){
Try {
Byte [] bytes = input. getBytes ("ISO8859-1 ");
Return new String (bytes, "GBK ");
}
Catch (Exception ex ){
}
Return null;
}
/**
* Return the HttpServletRequest holded by this object.
*/
Private HttpServletRequest getHttpServletRequest ()
{
Return (HttpServletRequest) super. getRequest ();
}
/**
* Read parameter -- corrected the Chinese problem.
*/
Public String getParameter (String name)
{
Return
ToChi (getHttpServletRequest (). getParameter (name ));
}
/**
* Read parameter list-fixed the Chinese problem.
*/
Public String [] getParameterValues (String name)
{
String values [] =
GetHttpServletRequest (). getParameterValues (name );
If (values! = Null ){
For (int I = 0; I <values. length; I ++ ){
Values [I] = toChi (values [I]);
}
}
Return values;
}
}
Public void destroy (){
}
Public void doFilter (ServletRequest request, ServletResponse
Response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpreq = (HttpServletRequest) request;
If (httpreq. getMethod (). equals ("POST ")){
Request. setCharacterEncoding ("GBK ");
} Else {
Request = new Request (httpreq );
}
Chain. doFilter (request, response );
}
Public void init (FilterConfig filterConfig) throws
ServletException {
}
}

Of course, remember to change web. xml
Copy codeThe Code is as follows:
<Filter>
<Filter-name> TomcatFormFilter </filter-name>
<Filter-class> filters. TomcatFormFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> TomcatFormFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>

The above two methods have been tested and are easy to use. New methods will be updated later ~

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.