日期:2014-05-18  浏览次数:20708 次

页面乱码问题,高手请帮忙!
<%@   page   pageEncoding= "UTF-8 "%>
<%@   page   contentType= "text/html;charset=UTF-8 "%>
<%@   taglib   uri= "/tags/struts-bean "   prefix= "bean "   %>
<%@   taglib   uri= "/tags/struts-html "   prefix= "html "   %>
<%@   taglib   uri= "/tags/struts-logic "   prefix= "logic "   %>
<%@   taglib   uri= "/tags/struts-tiles "   prefix= "tiles "   %>
<%@   taglib   uri= "/tags/struts-nested "   prefix= "nested "%>

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=UTF-8 ">
<title> TESTSTRUTS </title>
</head>
<body>

<html:form   action= "/hello ">
用户帐号: <html:text   property= "userName "/> <html:errors   property= "userName "/> <br/>
用户密码: <html:text   property= "userPasswrd "/> <html:errors   property= "userPasswrd "/> <br/>
<html:submit   value= "提交 "/> <html:cancel   value= "取消 "/>
</html:form>
<logic:present   name= "personbean "   scope= "request ">
<h1>
<bean:message   key= "hello.jsp.page.hello "/>
<bean:write   name= "personbean "   property= "userName "/>
</h1>
<h2>
<bean:write   name= "personbean "   property= "userPasswrd "/>
</h2>
</logic:present>
</body>
</html>

结果是:“用户名称:”变成了“鐢ㄦ埛甯愬彿锛&#65533;”
请问是什么原因?查了很多资料了!没有解决!

------解决方案--------------------
1.编码问题:
(1)首先确定JSP页面头部是否有: <%@ page contentType= "text/html; charset=GBK " %>
(2)类似这样的转码:
String param= new String(request.getParameter( "param ").getBytes( "ISO-8859-1 "), "GBK ");
(3)添加filter字符过滤器,具体做法:

先添加类:

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import org.apache.log4j.Logger;
import java.net.URLEncoder;

/** *//**
* 请求中中文字符串过滤类
*/

public class SetEncodingFilter
implements Filter ...{
private Logger logger=Logger.getLogger(this.getClass());

public void init(FilterConfig filterConfig) throws ServletException ...{
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException ...{
logger.i