servlet 跳转页面后response.getWriter().println输出的汉字乱码
publicclassloginextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServ...
public class login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<form action='/webtest/test' method='post'>");
out.println("用户名<input type='text' name='uname'/>");
out.println("密码<input type='password' name='pwd'/>");
out.println("<input type='submit' value='登录'/>");
out.println("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
this.doGet(request, response);
}
}
public class test extends HttpServlet {
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 str=request.getParameter("uname");
System.out.println(str);
out.println("用户名:"+str);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
this.doGet(request, response);
}
}
从login页面跳转到test页面的汉字就乱码了,但控制台输出的明明是汉字。直接打开test页面反而没乱码。 展开
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<form action='/webtest/test' method='post'>");
out.println("用户名<input type='text' name='uname'/>");
out.println("密码<input type='password' name='pwd'/>");
out.println("<input type='submit' value='登录'/>");
out.println("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
this.doGet(request, response);
}
}
public class test extends HttpServlet {
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 str=request.getParameter("uname");
System.out.println(str);
out.println("用户名:"+str);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
this.doGet(request, response);
}
}
从login页面跳转到test页面的汉字就乱码了,但控制台输出的明明是汉字。直接打开test页面反而没乱码。 展开
2个回答
展开全部
这个问题我以前遇到:
1、就是页面请求的方式是get方式的就会乱码,用post的方式就没这个问题。
2、还有就是设置web.xml里面如果配置了过滤器也要设置相应的编码,
3、将服务器的编码方式设置成相应的方式。
4、页面的编码要和后台java文件的编码的相同
如果你实在要使用get方式请求,建议你将中文先使用URLEncoder的encode方法转换两次传到后台然后再使用URLDecoder.decode解码两次就会得到你想要的传过来的信息。
为什么要转换两次因为第一次有些特殊的符号不能转换成unicode,比如#等符号,所以。。。你懂的
前台使用js转码两次也行,方法是encodeURIComponent你更懂得
更多追问追答
追问
我用表单提交是用post方式啊。
out.println("");
追答
你写在get方法里面的吧??????
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询