在使用post提交时经常出现乱码怎么解决呢
本篇文章为大家解答
使用前
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置内容为html格式
response.setContentType("text/html");
String uname = request.getParameter("uname");
System.out.println(uname);
PrintWriter out = response.getWriter();
out.print("<h2>"+uname+"欢迎进入<h2/>");
out.flush();
out.close();
}
结果:
使用后:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置内容为html格式
response.setContentType("text/html");
> //响应的中文设置
> response.setCharacterEncoding("gbk");
String uname = request.getParameter("uname");
System.out.println(uname);
PrintWriter out = response.getWriter();
out.print("<h2>"+uname+"欢迎进入<h2/>");
out.flush();
out.close();
}
结果:
附:解决方式代码
request.setCharacterEncoding(“utf-8”); //设置请求的字符编码集