首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

tomcat中的一个过滤器例证

2012-10-19 
tomcat中的一个过滤器例子感谢:http://www.iteye.com/wiki/Struts/578-Struts原理与实践(5)?和Google将所

tomcat中的一个过滤器例子

感谢:http://www.iteye.com/wiki/Struts/578-Struts原理与实践(5)?和Google

将所有的request的字符集也设置为UTF-8。虽然,我们可以在每个文件中加入这样的句子:request.setCharacterEncoding("UTF-8");来解决,但这样显得很麻烦。一种更简单的解决方法是使用filter。具体步骤如下:?

在mystruts\WEB-INF\classes目录下再新建一个名为filters的目录,新建一个名为:SetCharacterEncodingFilter的类,并保存在该目录下。其实,这个类并不要您亲自来写,可以借用tomcat中的例子。现将该例子的程序节选如下:?

  1. package?filters; ??
  2. import?java.io.IOException; ??
  3. import?javax.servlet.Filter; ??
  4. import?javax.servlet.FilterChain; ??
  5. import?javax.servlet.FilterConfig; ??
  6. import?javax.servlet.ServletException; ??
  7. import?javax.servlet.ServletRequest; ??
  8. import?javax.servlet.ServletResponse; ??
  9. import?javax.servlet.UnavailableException; ??
  10. ?
  11. public?class?SetCharacterEncodingFilter?implements?Filter?{ ??
  12. ??
  13. ??
  14.  //可以理解为默认编码,在web.xml中配置后读取存入
  15. ????protected?String?encoding?=?null; ??
  16. ??

  17. ????protected?FilterConfig?filterConfig?=?null; ??

  18. ????protected?boolean?ignore?=?true; ??
  19. ??
  20. ????public?void?destroy()?{ ??
  21. ??
  22. ????????this.encoding?=?null; ??
  23. ????????this.filterConfig?=?null; ??
  24. ??
  25. ????} ??
  26. ??
  27. ??
  28. ????public?void?doFilter(ServletRequest?request,?ServletResponse?response, ??
  29. ?????????????????????????FilterChain?chain) ??
  30. ????????throws?IOException,?ServletException?{ ??
  31. ??
  32. ????????//?Conditionally?select?and?set?the?character?encoding?to?be?used?
  33. //如果被忽略或者请求未设置编码,为了防止乱码,进行编码过滤??
  34. ????????if?(ignore?||?(request.getCharacterEncoding()?==?null))?{ ??
  35. //设置请求编码为web.xml中配置的编码类型
  36. ????????????String?encoding?=?selectEncoding(request); ??
  37. ????????????if?(encoding?!=?null) ??
  38. ????????????????request.setCharacterEncoding(encoding); ??
  39. ????????} ??
  40. ??
  41. ????????//?Pass?control?on?to?the?next?filter?
  42. //由FilterChain进行过滤处理,而不是该Filter??
  43. ????????chain.doFilter(request,?response); ??
  44. ??
  45. ????} ??
  46. ??
  47.  //主要从配置文件中读取配置项并进行赋值
  48. ????public?void?init(FilterConfig?filterConfig)?throws?ServletException?{ ??
  49. ??
  50. ????????this.filterConfig?=?filterConfig; ??
  51.   //从web.xml读取配置的编码,如gbk,utf-8等,赋值给encoding
  52. ????????this.encoding?=?filterConfig.getInitParameter("encoding");??
  53.   //从web.xml读取是否忽略
  54. ????????String?value?=?filterConfig.getInitParameter("ignore");??
  55. ????????if?(value?==?null)??
  56. ????????????this.ignore?=?true;??
  57. ????????else?if?(value.equalsIgnoreCase("true"))??
  58. ????????????this.ignore?=?true;??
  59. ????????else?if?(value.equalsIgnoreCase("yes")) ??
  60. ????????????this.ignore?=?true; ??
  61. ????????else??
  62. ????????????this.ignore?=?false; ??
  63. ??
  64. ????} ??
  1. ?protected?String?selectEncoding(ServletRequest?request)?{ ??
  2. ??
  3. ????????return?(this.encoding); ??
  4. ??
  5. ????} ??
  6. ??
  7. }?
  1. <filter>??
  2. ????<filter-name>Set?Character?Encoding</filter-name>??
  3. ????<filter-class>filters.SetCharacterEncodingFilter</filter-class>??
  4. ????<init-param>??
  5. ??????<param-name>encoding</param-name>??
  6. ??????<param-value>UTF-8</param-value>??
  7. ????</init-param>??
  8. ??</filter>??
  9. ??<filter-mapping>??
  10. ????<filter-name>Set?Character?Encoding</filter-name>??
  11. ????<url-pattern>/*</url-pattern>??
  12. </filter-mapping>??

热点排行