MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » 页面乱码有关问题,高手请帮忙

页面乱码有关问题,高手请帮忙

www.MyException.Cn  网友分享于:2013-03-20  浏览:5次
页面乱码问题,高手请帮忙!
<%@   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.info( "请求转码过滤器=================== ");
request.setCharacterEncoding( "gb2312 ");
chain.doFilter(request,response);
}

public void destroy() ...{
}
}
再注册类到XML里:

<filter>
<filter-name> Set Encoding </filter-name>
<filter-class> SetEncodingFilter </filter-class>
</filter>
<filter-mapping>

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有