Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2321164
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2011-12-20 11:03:51

 
1  web.xml的过滤器配置
 
  1. <filter>
  2. <filter-name>EncodingFilter</filter-name>
  3. <filter-class>com.utils. EncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>gb2312</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>EncodingFilter</filter-name>
  11. <url-pattern>/*</url-pattern> </filter-mapping>

2  EncodingFilter.java

 

  1. package com.utils;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class EncodingFilter extends HttpServlet implements Filter {
  5.      private FilterConfig config = null;
  6.      private String encoding = "";
  7.      public void init(FilterConfig config) throws ServletException {
  8.            this.config = config;
  9.            this.encoding = config.getInitParameter("encoding");
  10.      }
  11.      public void doFilter(ServletRequest request, ServletResponseresponse, FilterChain chain) throws java.io.IOException, ServletException {
  12.      HttpServletRequest req = (HttpServletRequest)request;
  13.      HttpServletResponse res = (HttpServletResponse)response;
  14.      response.setCharacterEncoding(this.encoding);
  15.      java.io.PrintWriter out = res.getWriter();
  16.      out.print("过滤器设置编码为:"+this.encoding+"

    "
    ); chain.doFilter(req, res);
  17. }
  18. }

这样 每个页面的最前面都会出现一句话 过滤器设置编码为gb2312

 

 

阅读(1803) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~