1、今天写了给程序,使用struts2 + android进行开发,结果程序出现乱码的情况。
解决乱码首先要考虑的步奏
1、是get方式出现乱码,还是post方式出现乱码
get方式下出现乱码,通常情况下是客户端提交的编码和服务器接受的编码不一致。
android默认提交编码为UTF8编码,如果是get方式出现乱码,只需要修改tomcat钟的server.xml配置文件,设置编码为UTF-8编码,如下图
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
设置服务器编码后,get方式乱码问题,通常解决。
2、post方式乱码,主要是查看web.xml中是否配置编码过滤器等
一、Tomcat默认编码ISO8859_1修改为UTF-8
由于tomcat这款软件不是中国人开发的,所以他的默认编码格式是:iso-8859 所以只有通过在服务器配置文件中配置编码,系统才能以不出现乱码。
其实很简单,只是每次遇到总忘记在哪里配,怎么配,记录在这里,方便以后查找~
在tomcat/conf/server.xml中找到 Connector 在后面加上 URIEncoding=”UTF-8〃 即可
<Service name="Catalina">
...
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
...
二、tomcat默认编码是ISO8859_1,如果不修改默认编码,得到参数的JSP需要转换下:
String entityNames = request.getParameter("names");
entityNames = new String(entityNames.getBytes("ISO8859_1"),"utf-8");