request中文问题

wdhs 2004-08-24 06:24:58
服务器:resin3.0.8

jsp中:
request.getCharacterEncoding() == "UTF-8",
String sWords = request.getParameter("words")显示中文正常

但在servlet中:
request.getCharacterEncoding() == null,
response.setCharacterEncoding("utf-8");
out.println(sWords+"<br/>");
out.println(new String(sWords.getBytes("unicode"),"utf-8")+"<br/>");
out.println(new String(sWords.getBytes("gb2312"),"utf-8")+"<br/>");
out.println(new String(sWords.getBytes("ISO8859-1"),"utf-8")+"<br/>");
显示中文都不正常

大虾们帮忙把把脉!谢谢。
...全文
187 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hary90 2004-08-25
  • 打赏
  • 举报
回复
其实这些编码问题我都用一个过滤器就可以搞定。
wangxiaomax 2004-08-25
  • 打赏
  • 举报
回复
使用filter
web.xml
<filter>
<filter-name>characterfilter</filter-name>
<filter-class>CharacterFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

CharacterFilter.java

public class CharacterFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(selectEncoding(request));
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
public FilterConfig getFilterConfig() {
return filterConfig;
}
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
}
eureka0891 2004-08-25
  • 打赏
  • 举报
回复
你可以看一下jsp生成的servlet里是怎么设的就行了!
xiangbo520 2004-08-24
  • 打赏
  • 举报
回复
response.setCharacterEncoding("utf-8");你的是这样的,是错的啊!应该是request.setCharacterEncoding("utf-8");
dlxu 2004-08-24
  • 打赏
  • 举报
回复
好像要先把从HTML页面传来的数据从ISO8859-1转换为byte,然后再转换成需要的编码的
xuyang821225 2004-08-24
  • 打赏
  • 举报
回复
request.setCharacterEncoding("utf-8");
改成这个试试
gln 2004-08-24
  • 打赏
  • 举报
回复
试试GBK编码
longjing_g 2004-08-24
  • 打赏
  • 举报
回复
莫非servlet不支持

81,094

社区成员

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

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