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

servlet编码乱码问题

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

标签:tac   blog   htm   tco   enc   方法   character   输出   logs   

继承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编码乱码问题

标签:tac   blog   htm   tco   enc   方法   character   输出   logs   

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

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