[顶]治根又治本解决中文问题

aimomo 2009-01-10 07:44:47
中文问题

No.1
提交方式为 post的首先要把页面上的PageEncoding="UTF-8";
然后可以在servlet里头先设置request/response.setCharacterEncoding("UTF-8");
最后就是如果重新指定到别外一个页面,那么也要把要指定的那个页面设置PageEncoding="UTF-8"

//解决get提交方式的中文问题(1)//还原->>组装
public String getCharset(String str)
{
String strfirst=null;
byte[] charsetbyte=null;
//还原
try {
charsetbyte=str.getBytes("ISO-8859-1"); //注意:默认进servlet的编码方式就是ISO-8859-1,而不是按照页面的编码格式
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//重新组装
try {
strfirst=new String(charsetbyte,"utf-8"); //这个类型要与请求页面的编码一致
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strfirst;
}

No.2
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
其中pageEncoding是设置本页的编码方式与其它页面、Servlet无关。

No.3
如果页面要跳转的话;例如:
request.getRequestDispatcher("/Userjsp/UpdatePwd.jsp").forward(request, response);
response.sendRedirect("Userjsp/fail.jsp");
这些跳转的话,提交的方式是post所以可以用过滤器来解决.
if(this.encoding!=null)
{
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
}
chain.doFilter(request, response); //关键代码哇!!
注意web.xml里的配置
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.weichen.filter.CharsetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...全文
58 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
DLM8207 2009-01-15
  • 打赏
  • 举报
回复
aimomo 2009-01-14
  • 打赏
  • 举报
回复


呆呵呵`~`

本人还要向前辈们多多学习呢``
aimomo 2009-01-14
  • 打赏
  • 举报
回复


呆呵呵`~`

本人还要向前辈们多多学习呢``
lihan6415151528 2009-01-10
  • 打赏
  • 举报
回复
String str=new String("治根又治本解决中文问题").getBytes("iso-8859-1"),"gb2312";
Ami121 2009-01-10
  • 打赏
  • 举报
回复
支持
aimomo 2009-01-10
  • 打赏
  • 举报
回复

呵呵、、
spring 和structs这些都没有学呢`
blueram 2009-01-10
  • 打赏
  • 举报
回复
收藏了,不错
楼主把和spring集成后spring的乱码处理配置

struts2的乱码处理情况写出来就全了,呵呵
aimomo 2009-01-10
  • 打赏
  • 举报
回复


嗯、、嗯。。
老紫竹 2009-01-10
  • 打赏
  • 举报
回复
继续总结吧。这几个都是很常见的东西。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧