In the preparation of JSP programs, often encountered in the Chinese character processing problems, in the acceptance of request in the Chinese characters display a string of garbled. Online processing Method A basket, the following say I used two 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.
<%!
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.
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.
<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 ~