package test; importjava.io.IOException; importjava.io.PrintWriter; importjava.io.UnsupportedEncodingException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; public classUserMsg extends HttpServlet{ public void doGet(HttpServletRequestrequest, HttpServletResponse response){ doPost(request, response); } public void doPost(HttpServletRequestrequest, HttpServletResponse response){ try { request.setCharacterEncoding("gb2312"); } catch (UnsupportedEncodingExceptione) { e.printStackTrace(); } PrintWriter out = null; try { out = response.getWriter(); } catch (IOException e1) { e1.printStackTrace(); } out.print("<html>"); out.print("<body>"); out.print("<h2>" +"用户信息:"+ "</h2>"); out.print("<p>"+"邮箱:"+request.getParameter("email")+"<p/>"); out.print("<p>"+"昵称:"+request.getParameter("nickname")+"<p/>"); out.print("<p>"+"密码:"+request.getParameter("password")+"<p/>"); out.print("<p>"+"性别:"+request.getParameter("sex")+"<p/>"); out.print("<p>"+"个人介绍:"+request.getParameter("introduction")+"<p/>"); out.print("</html>"); out.print("</body>"); } }
运行结果:
解决方法:在doPost中加入:
response.setContentType("text/html; charset=gb2312");
以上就是几种常见JSP中文乱码例子及其解决方法,希望能够帮助大家解决JSP中文乱码的问题。
您可能感兴趣的文章: