表单文件<%@ page contentType="text/html; charset=gb2312" language="java"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>帐号密码</title>
</head><body>
<form action="../addDo" method="post">
    姓名<input type="text" name="name"  /><br />
    密码<input type="text" name="password" />
    <input type="submit" value="确定" />
    
</form>
</body>
</html>
 处理文件
package mypack;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class addDo extends HttpServlet
{
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
    {
        PrintWriter out = response.getWriter();
       
        //response.setCharacterEncoding("gb2312");把这里的注释去掉也不行
        
        out.println("姓名:"+request.getParameter("name"));
        out.println("密码:"+request.getParameter("password"));
    }
}输入:
张三
123456输出:
??:张三 ??:123456 
也就是说,传递的参数没有乱码,但是addDo本身打印的中文乱码了。
另外 把addDo的注释去掉也不行。这是怎么回事呢?
应该咋办?
谢谢!

解决方案 »

  1.   

    数据库什么编码?
    保持一直的地方:
    jsp页面
    request response时 建议用filter
    编辑器编码
    db编码
      

  2.   

    加上
    response.setContentType("text/html; charset=GBK");
      

  3.   

    <%@ page pageEncoding="gb2312"%>加上
      

  4.   

    addDo.java这个文件自身不是GBK编码的。如果你使用Eclipse ,看看这个文章 http://www.java2000.net/viewthread.jsp?tid=580
      

  5.   

    补充下 在网页源码查看到的同上面的一样。
    就是传递的中文参数没有乱码 但是addDo本身打印的全部是乱码!!!!!!!!!!!!
      

  6.   

     response.setCharacterEncoding("gb2312");//放在前面
    request.setCharacterEncoding("gb2312");//也加上 就可以了
            PrintWriter out = response.getWriter();
           
           
            
            out.println("姓名:"+request.getParameter("name"));
            out.println("密码:"+request.getParameter("password"));
      

  7.   

     response.setCharacterEncoding("gb2312");//响应数据中文处理
    request.setCharacterEncoding("gb2312");//请求数据中文处理
      

  8.   

     response.setCharacterEncoding("gb2312");
    request.setCharacterEncoding("gb2312");  这二句放JSP中看看.
      

  9.   

    1.传参数之前用文本框用trim()方法2.request.getParameter("name")改成
     new String(request.getparameter("name").getBytes("ISO-8859-1"),"GB2312")3.加入下两句,都要加上
    request.setCharacterEncoding("gb2312");       
    response.setContentType("text/html;charset=gb2312"); 1 or 2 or 3看看
      

  10.   


    <%@ page contentType="text/html; charset=gb2312" language="java"  %>
    把charset这里改成utf-8试下!
      

  11.   

    此法可以解决,
    但最好不要用gbk,utf8比较好