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

servlet的编码问题!网上搜的都试了,还是不行!不知道咋回事?!
以前我是用jsp+javaBean,这些代码在jsp中没事,在jsp中的编码是<%@ page language="java" pageEncoding="GBK"%>
但写到servlet中就不行了?userName不能正确显示

  response.setContentType("text/html"); 
response.setCharacterEncoding("GBK"); 

String user = Convert.gbToISO(request.getParameter("userName"));
String password = request.getParameter("password");
PrintWriter out = response.getWriter();

out.println("用户注册成功!");
out.println("用户:"+user);
out.println("密码:"+password);
//response.sendRedirect("/product/success.jsp");
out.flush();
out.close();
怎么回事?请指点

------解决方案--------------------
还加一句
response.setContentType("text/html"); 
response.setCharacterEncoding("GBK"); 
request.setCharacterEncoding("GBK");
------解决方案--------------------
先在系统中输出相关值,从而确认是request还是reponse乱码。第二点确认是用什么方式提交?post还是get,第三确认编辑工具保存的默认编码。第四确认jsp设置的编码。第五点确认你的第统编码。不过是可能的我想你可能是request出现的乱码。你可以实用强转
String str=new String(str.getByte("ISO-8859-1","GBK"))
不太记的参数值了。大概是这样的吧。自已查下
------解决方案--------------------
加上request.setCharacterEncoding("GBK");