日期:2014-05-19  浏览次数:20791 次

新手求教乱码问题
刚开始学习servlet,写了一个表单提交,结果提交的结果是乱码,请指教:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();

out.println("<html><body>");
out.println("<form action = '/Test2/Resieve' mathod='post'>");
out.println("用户名:<input type = 'text' name='username' /><br/>");
out.println("密 码:<input type = 'password' name = 'password' /><br/>");
out.println("性 别:<input type='radio' name='sex' value='男' />男<input type='radio' name='sex' value='女' />女<br/>");
out.println("个人爱好:<input type='checkbox' name='habit' value='英语' />英语<input type='checkbox' name='habit' value='物理' />物理<input type='checkbox' name='habit' value='语文' />语文<input type='checkbox' name='habit' value='数学' />数学<br/>");
out.println("所在城市:<select name='city'><option value='北京'>北京</option><option value='银川'>银川</option></select><br/>");
out.println("个人介绍:<textarea cols='10' rows='10' name='introduce' ></textarea>");
out.println("<input type='hidden' name='va' value='隐藏参数'>");
out.println("<input type='submit' value='提交'>");
out.println("</form>");
out.println("</body></html>");

}



public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();

String username = request.getParameter("username");
out.println("您的姓名是 "+username);

String password = request.getParameter("password");
out.println("您的密码是 "+ password);

String sex = request.getParameter("sex");
out.println("您的性别是 "+ sex);

String habit[] = request.getParameterValues("habit");
for(int i = 0 ; i < habit.length ; i ++){

out.print("爱好" + habit[i]+" ");
}

String introduce = request.getParameter("introdce");
out.println("个人介绍:"+introduce);

String city = request.getParameter("city");
out.println("所在城市:"+city);

String hidden = request.getParameter("va");
out.println("隐藏的字符时:"+hidden);

}


------解决方案--------------------
编码方式不用UTF-8试试,或者换个编码方式储存。
------解决方案--------------------
要看jsp里的编码 是否统一了
------解决方案--------------------
http://blog.csdn.net/lip009/article/details/1192022
乱码请参考