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. xmlCopy 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 ~