继承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     }

 

posted on 2017-10-20 20:07  nicole01  阅读(132)  评论(0编辑  收藏  举报