码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-19 17:52:54      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:string   exception   中文乱码   tps   登录密码   public   this   登录   .com   

如果在登录界面跳转到时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 }

技术分享

技术分享

 

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

标签:string   exception   中文乱码   tps   登录密码   public   this   登录   .com   

原文地址:http://www.cnblogs.com/limn/p/7206572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!