Servlet中request、response 设置乱码问题与解决方式

一、request乱码

  • 设置请求的编码

request.setCharacterEncoding("utf-8”);

只能解决表单方法为POST的中文乱码情况,而方法为GET的依然为乱码

解决GET乱码方法:

String str=new String(request.getParameter("变量名").getBytes("ISO8859-1"),"UTF-8");

二、response乱码

  • 通知服务器发送数据时使用utf-8编码

response.setCharacterEncoding("utf-8”);

  • 通知浏览器接收数据时使用utf-8解码

response.setHeader("Content-Type", "text/html;charset=utf-8");

  • response对象中对Content-Type响应头进行了封装,可以使用以下代码代替

response.setContentType("text/html;charset=utf-8”);

你可能感兴趣的:(Servlet中request、response 设置乱码问题与解决方式)