JSP Chinese character processing code
- 2020-05-24 05:58:12
- OfStack
Internet processing method 1 basket, I have used the following two effective solutions.
1. Write a string manipulation function for the program, save it in a static file, and include it in the JSP page where Chinese characters need to be processed.
To convert: coedeToString(request.getParameter (..)) ); That's it. It works.
2.1 a universal method, add a filter, and you can live for good.
Of course, remember to change web.xml
The above two methods have been tested, is relatively easy to use, later found new, will be updated oh ~
1. Write a string manipulation function for the program, save it in a static file, and include it in the JSP page where Chinese characters need to be processed.
<%!
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: coedeToString(request.getParameter (..)) ); That's it. It works.
2.1 a universal method, add a filter, and you can live for good.
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
* right HttpServletRequestWrapper To expand , Does not affect the original function and can provide
some HttpServletRequest
* Functions in the interface . It can be unified 1 the Tomcat The default Settings for the Chinese problem to be resolved and only
You need a new one Request Object to replace the page
* request Objects can be .
*/
class Request extends HttpServletRequestWrapper
{
public Request(HttpServletRequest request) {
super(request);
}
/**
* Converts the internal code of the data read by the form .
* 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 the parameter -- Fixed a problem with Chinese .
*/
public String getParameter(String name)
{
return
toChi(getHttpServletRequest().getParameter(name));
}
/**
* Read parameter list - Fixed a problem with Chinese .
*/
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
<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, is relatively easy to use, later found new, will be updated oh ~