Fork me on GitHub

中文乱码问题解决方法总结

  

//我遇到的是第一个问题,顺利解决,搞定,基本这几种方法就够用了。

1.中文乱码:
1)tomcat 改成 utf-8   URIEncoding="UTF-8"
D:\Tomcat 6.0\conf中的server.xml中修改URIEncoding="UTF-8"
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

2)加 过滤器

EncodeFilter.java
具体步骤是:
1.先在web.xml配置文件中增加代码:

 1 <!--编码过滤器-->
 2 
 3 <filter>
 4 <filter-name>encodeFilter</filter-name>
 5 <filter-class>common.EncodeFilter</filter-class>
 6 <init-param>
 7 <param-name>encoding</param-name>
 8 <param-value>utf-8</param-value>
 9 </init-param>
10 </filter>
11 
12 
13 <filter-mapping>
14 <filter-name>encodeFilter</filter-name>
15 <url-pattern>/*</url-pattern>
16 </filter-mapping>
View Code

 

2.增加一个过滤用的类EncodeFilter.java

 1 package common;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 
12 import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
13 
14 public class EncodeFilter extends StrutsPrepareAndExecuteFilter implements Filter {
15 private FilterConfig config = null;
16 private String encoding = null;
17 
18 @Override
19 public void init(FilterConfig config) throws ServletException {
20 this.config = config;
21 }
22 
23 @Override
24 public void doFilter(ServletRequest request, ServletResponse response,
25 FilterChain chain) throws IOException, ServletException {
26 if (encoding == null) {
27 encoding = config.getInitParameter("encoding");
28 }
29 request.setCharacterEncoding(encoding);
30 response.setCharacterEncoding(encoding);
31 chain.doFilter(request, response);
32 }
33 
34 @Override
35 public void destroy() {
36 config = null;
37 encoding = null;
38 }
39 }

 

 


3 表单提交的时候使用post方式
表单提交的时候,myeclipse默认是用的<form action="air!cp.action" method="get">
你要改成post方法
ps

注意到表单中action:getPostServlet/getPost.do?param4=param4
这个action带有一个参数param、,
如果用get方法提交,后台无法接收到这个参数;
如果用post方法提交,后台就可以接收到这个参数

posted @ 2015-08-16 19:47  ZZZZW  阅读(1033)  评论(0编辑  收藏  举报
AmazingCounters.com