解决web项目开发中常见的中文乱码问题
Java代码
1. 经常在论坛中看到朋友遇到中文乱码的问题而不知所措,本人以自己在项目中常遇到的中文乱码问题及解决方法,小结一下,希望能对朋友有所帮助,如有不足,请指正。
2.
3. 总的归纳有以下几个方面:
4.
5. 一、在表单正常提交后,页面显示出乱码。
6.
7. 解决方法:
8.
9. 1、如果是直接jsp页面提交到别外一个jsp页面,或jsp提交到servlet,可以在jsp或serlet的doPost()或doGet()方法设置编码转换,将请求后的数据转成中文,并在输出时也设置为中文如:
10.
11. request.setCharacterEncoding("GBK");
12. response.setCharacterEncoding("GBK");
13.
14. 2、设置过滤器(推荐方法):
15.
16. 写一个过滤器类,并且建议写在静态类,方便调用,如下:
17.
18.
19. [java]view plaincopyprint?
20. /**
21. * 全局编码过滤器,
22. *
23. * @author fafuhefan
24. *
25. */
26. publicclassEncodingFilterimplementsFilter {
27. String encoding ="GBK";
28.
29. publicvoiddestroy() {
30.
31. }
32.
33. publicvoiddoFilter(ServletRequest request, ServletResponse response,
34. FilterChain chain)throwsIOException, ServletException {
35.
36. HttpServletRequest request_1 = (HttpServletRequest) request;
37. HttpServletResponse response_1 = (HttpServletResponse) response;
38.
39.
40. request_1.setCharacterEncoding(encoding);
41. response_1.setCharacterEncoding(encoding);
42. chain.doFilter(request, response);
43.
44. }
45.
46. publicvoidinit(FilterConfig config)throwsServletException {
47.
48. String str = config.getInitParameter("encoding");
49. if(str !=null&& str.length() >0) {
50. encoding = str;
51. }
52. }
53.
54. }
55. /**
56. * 全局编码过滤器,
57. *
58. * @author fafuhefan
59. *
60. */
61. public class EncodingFilter implements Filter {
62. String encoding = "GBK";
63.
64. public void destroy() {
65.
66. }
67.
68. public void doFilter(ServletRequest request, ServletResponse response,
69. FilterChain chain) throws IOException, ServletException {
70.
71. HttpServletRequest request_1 = (HttpServletRequest) request;
72. HttpServletResponse response_1 = (HttpServletResponse) response;
73.
74.
75. request_1.setCharacterEncoding(encoding);
76. response_1.setCharacterEncoding(encoding);
77. chain.doFilter(request, response);
78.
79. }
80.
81. public void init(FilterConfig config) throws ServletException {
82.
83. String str = config.getInitParameter("encoding");
84. if (str != null && str.length() > 0) {
85. encoding = str;
86. }
87. }
88.
89. }
90.
91. 并在web.xml作如下配置:
92.
93. <filter>
94. <filter-name>EncodingFilter</filter-name>
95. <filter-class>com.crm.filter.EncodingFilter</filter-class>(说明:此为类路径)
96. <init-param>
97. <param-name>encoding</param-name>
98. <param-value>GBK</param-value>
99. </init-param>
100. </filter>
101. <filter-mapping>
102. <filter-name>EncodingFilter</filter-name>
103. <url-pattern>/*</url-pattern>
104. </filter-mapping>
105.
106. 二、如果为url后在跟的参数为中文,如:xxx.do?name=你好,或采用自动跳转链接时,后面跟的提示信息含有中文。
107.
108. 解决方法:
109.
110. 1、可以写个类,进行转换如:
111.
112.
113.
114. [java]view plaincopyprint?
115. /**
116. * 将编码转成中文编码GBK
117. * @author fafuhefan
118. * 返回中文字符串
119. */
120. publicclasstools {
121.
122. /**
123. * @param str
124. * 需要转换的字符串
125. * @return
126. * 中文字符串
127. */
128. publicstaticString toGBK(String str) {
129. try{
130. str = java.net.URLEncoder.encode(str,"gb2312");
131. }catch(UnsupportedEncodingException e) {
132. e.printStackTrace();
133. }
134. returnstr;
135. }
136.
137. }
138. /**
139. * 将编码转成中文编码GBK
140. * @author fafuhefan
141. * 返回中文字符串
142. */
143. public class tools {&n
补充:Web开发 , 其他 ,