设置编码和解码都为同一字符集如UTF-8.
1、设置工作空间的编码方式:window-preferences-general-wordspace
2、设置jsp文件的编码方式:windowpreferences-web-jsp files
3、对应post和get 提交数据的方式,分别有两中设置方式:
对应post
A使用new String()方式:
例:Stirng name = request.getParameter("username");
name= new String(name.getBytes("iso-8859-1"),"utf-8");
B、设置请求和响应的编码方式:
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");注意;page指令中设置了utf-8后,后一句可以省略。
对应get方式:
a使用new String()方式
B、修改tomcat配置文件:
tomcat/conf/server.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8"/>
4、对cookie对象:
需要单独设置:
例如:Cookie cookie= new Cookie("name","中国");
设置后Cookie cookie = new Cookie("name",URLEncoder.encode("中国"));
response.addCookie(cookie);将"中国"设置为英文编码后存储。
读取时:name=cookie[i].getValue();
name=URLDecoder.decode(name);读取时,将其解码为中文字符。