servlet编码乱码问题

继承HttpServlet

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

post提交时:

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

Get方法

继承extends HttpServletRequestWrapper

重写getParameter()方法

 @Override
public String getParameter(String name) {
String value=request.getParameter(name);
String method=request.getMethod();
if("get".equalsIgnoreCase(method)){
try {
value=new String(value.getBytes("iso-8859-1"),"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return value;
}
上一篇:day26 多继承


下一篇:ssh自动登录,脚本实现