这段时间又转到web开发了,今天看到了怎么样处理中文乱码问题,赶紧记下,以下回顾。
解决中文乱码问题要分request 和 response两种。
response要返回中文需要在response.getWriter()之前调用
response.setContentType("text/html;charset=GBK");
或者
response.setCharacterEncoding("GBK"):
比较简单吧。。
解决request从页面中得到中文参数的问题要分post 和get 两种方式。
post 方法得到 参数解决乱码:
在存取第一个参数之前加上
request.setCharacterEncoding("GBK");
这样便能从上一个页面下以post的方式获取中文参数。
get方法:
除了上述操作之外还得改tomcat的配置文件server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="GBK"/>
找到上面那句话,红色部分是需要添加的。
可能还会有其它的中文乱码问题,以后碰到再过来改。