文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用Servlet Filter消除struts乱码

使用Servlet Filter消除struts乱码

时间:2010-09-25  来源:日光倾城。

先创建一个Java类,继承自 javax.servlet.Filter :

 1 package com.test.util;  
2
3 import javax.servlet.Filter;
4 import javax.servlet.FilterConfig;
5 import javax.servlet.ServletException;
6 import javax.servlet.ServletRequest;
7 import javax.servlet.ServletResponse;
8 import javax.servlet.FilterChain;
9 import java.io.IOException;
10
11 public class SetCharacterEncodingFilter implements Filter {
12
13 protected FilterConfig filterConfig;
14 protected String encodingName;
15 protected boolean enable;
16
17 public SetCharacterEncodingFilter() {
18 this.encodingName = "UTF-8";
19 this.enable = false;
20 }
21
22 public void init(FilterConfig filterConfig) throws ServletException {
23 this.filterConfig = filterConfig;
24 loadConfigParams();
25 }
26
27 private void loadConfigParams() {
28 this.encodingName = this.filterConfig.getInitParameter("encoding");
29 String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
30 if (strIgnoreFlag.equalsIgnoreCase("true")) {
31 this.enable = true;
32 } else {
33 this.enable = false;
34 }
35 }
36
37 public void doFilter(ServletRequest request, ServletResponse response,
38 FilterChain chain) throws IOException, ServletException {
39 if (this.enable) {
40 request.setCharacterEncoding(this.encodingName);
41 response.setCharacterEncoding(this.encodingName);
42 }
43 chain.doFilter(request, response);
44 }
45
46 public void destroy() {
47 }
48 }

 

然后在web.xml中加入Filter的描述和参数配置:
 1 <filter>  
2 <filter-name>SetCharacterEncoding</filter-name>
3 <filter-class>com.test.util.SetCharacterEncodingFilter</filter-class>
4 <init-param>
5 <param-name>encoding</param-name>
6 <param-value>UTF-8</param-value>
7 </init-param>
8 <init-param>
9 <param-name>enable</param-name>
10 <param-value>true</param-value>
11 </init-param>
12 </filter>
13 <filter-mapping>
14 <filter-name>SetCharacterEncoding</filter-name>
15 <servlet-name>action</servlet-name>
16 </filter-mapping>

 


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载