Servlet----------在使用doGet()和doPost()是如何处理中文乱码

如果在登录界面跳转到时servlet中使用doGet()方法显示中文出现乱码时,可以添加这两行代码来解决,但这不是唯一的方法

request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK")

 

如我的

 1 package cn.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class Login extends HttpServlet {
12     public void doGet(HttpServletRequest request, HttpServletResponse response)
13             throws ServletException, IOException {
14         request.setCharacterEncoding("GBK");//解决乱码
15         response.setContentType("text/html;charset=GBK");//解决乱码
16         
17         String name=request.getParameter("login");
18         String password=request.getParameter("password");
19         response.setCharacterEncoding("GBK");
20         PrintWriter out = response.getWriter();
21         out.println("欢迎-- "+name+" --登录!"+"</br>");
22         out.print("你的登录密码是:"+password);    
23     }
24     public void doPost(HttpServletRequest request, HttpServletResponse response)
25             throws ServletException, IOException {
26         this.doGet(request, response);
27     }
28 }

 

posted @ 2017-07-19 16:42  Kmily_Lee  阅读(3152)  评论(0编辑  收藏  举报