多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 简介 乱码问题 浏览器编码格式由当前打开的页面决定,如果页面中设置了编码格式.那么浏览器就使用这个,如果没有,浏览器使用系统默认编码. 服务器编码格式和解码格式默认就是ISO-8859-1 浏览器的解码格式由当前打开的页面决定 浏览器(编码)---请求--->服务器(解码) 服务器(编码)---响应--->浏览器(解码) # 配置 ## 修改tomcat编码 在server.xml中配置 ~~~ <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ~~~ 找到这行,这行可以配置tomcat的端口. 在里面加编码 ~~~ <Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ~~~ 但是这边的编码只能解决get中查询字符串中的乱码 ## 设置编码 分别设置请求响应编码 ~~~ //设置请求编码 request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); System.out.println(name); //设置响应编码 response.setCharacterEncoding("UTF-8"); //这边设置了utf-8编码,有些浏览器不知道如何解析,我们要告诉浏览器,所以我们还要设置个响应头 response.setHeader("Content-Type", "text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(name); ~~~