请求访问时的乱码问题

来源:互联网 发布:借乎扫描不了身份证 编辑:程序博客网 时间:2024/06/10 10:59

请求访问出现乱码可能是在以下方面:

  • request
    • post
    • get
  • response

post方法请求访问:

设置request.setCharacterEncoding("utf-8");这样就可以解决post方法

get方法请求访问:

方法一:

username = new String(username.getBytes("ISO-8859-1"),"UTF-8");

其中username是通过getParameter方法获取的值,原理是:先将值按照encode的编码去decode,得到的字节数组再按照utf-8来encode,这样就识别中文了

方法二:

找Servers应用下面的server.xml文件,大概在60几行添加 URIEncoding="utf-8" 这个属性设置

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"    URIEncoding = "UTF-8"/>

第二个方法比较常用

response响应请求:

直接在输出(返回响应)之前设置

response.setContentType("text/html;charset=utf-8");
就可以解决乱码问题


原创粉丝点击