JSP Chinese character processing code _jsp programming

Source: Internet
Author: User
Online processing Method A basket, the following say I used two kinds of effective solutions.
1. Write a string handler function for the program, save it in a static file, and include it in the JSP page that needs to handle the Chinese characters.
Copy Code code 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, just: coedetostring (Request.getparameter (..)); On it, it's good to use.
2. A universally applicable method, add a filter, then you can live once and for all.
Copy Code code 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
* The expansion of the httpservletrequestwrapper does not affect the original function and can provide the
Some httpservletrequest
* Functions in the interface. It can be unified to solve the Chinese problem under the Tomcat default setting and only
The new Request object needs to be replaced with the
* Request object can be.
*/
Class Request extends Httpservletrequestwrapper
{
Public request (HttpServletRequest request) {
Super (Request);
}
/**
* Converts the inner code of the data that is read by the form.
* Transfer from 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 Parameters--fixed the Chinese problem.
*/
public string GetParameter (string name)
{
Return
Tochi (Gethttpservletrequest (). GetParameter (name));
}
/**
* Read the parameter list-fixed the Chinese issue.
*/
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 the web.xml.
Copy Code code 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, more useful, later found new, will be updated Oh ~
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.