首页 > 其他 > 详细

servlet编码乱码问题

时间:2017-10-20 21:05:00      阅读:341      评论:0      收藏:0      [点我收藏+]

继承HttpServlet

重写doGet(HttpServletRequest req,HttpServletResponse resp),doPost()方法

post提交时:

1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2         //post方法:告诉servlet如何去处理文字编码,输出
3         response.setCharacterEncoding("utf-8");
4         //告诉浏览器如何处理编码
5         response.setContentType("text/html;charset=tf-8");
6     }

Get方法

继承extends HttpServletRequestWrapper

重写getParameter()方法

 1 @Override
 2     public String getParameter(String name) {
 3         String value=request.getParameter(name);
 4         String method=request.getMethod();
 5         if("get".equalsIgnoreCase(method)){
 6             try {
 7                 value=new String(value.getBytes("iso-8859-1"),"utf-8");
 8             } catch (Exception e) {
 9                 e.printStackTrace();
10             }
11         }
12         return value;
13     }

 

servlet编码乱码问题

原文:http://www.cnblogs.com/xiaochenxi/p/7701302.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!