connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//编写过滤器,需要实现javax.servlet.Filter接口
public class CharactorEncodingFilter implements Filter
{
public void destroy()
{
}
//在doFilter方法中处理
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
//将所有request内的对象设置字符集为UTF-8
request.setCharacterEncoding("UTF-8");
//用chain的doFilter处理过滤
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException
{
}
}
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]">
<!-- 在web.xml文件中增加对Filter的设置 -->
<filter>
<!-- filter的名称 -->
<filter-name>charactorEncoding</filter-name>
<!-- filter对应的类 -->
<filter-class>
com.meixin.filter.CharactorEncodingFilter
</filter-class>
</filter>
<!-- 设置过滤器调用的映射 -->
<filter-mapping>
<!-- filter的名称,和设置中的名称要一致 -->
<filter-name>charactorEncoding</filter-name>
<!-- 设置对当前的request,response请求都进行过滤 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>