1 web.xml的过滤器配置
- <filter>
- <filter-name>EncodingFilter</filter-name>
- <filter-class>com.utils. EncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>gb2312</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>EncodingFilter</filter-name>
- <url-pattern>/*</url-pattern> </filter-mapping>
2 EncodingFilter.java
- package com.utils;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class EncodingFilter extends HttpServlet implements Filter {
- private FilterConfig config = null;
- private String encoding = "";
- public void init(FilterConfig config) throws ServletException {
- this.config = config;
- this.encoding = config.getInitParameter("encoding");
- }
- public void doFilter(ServletRequest request, ServletResponseresponse, FilterChain chain) throws java.io.IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest)request;
- HttpServletResponse res = (HttpServletResponse)response;
- response.setCharacterEncoding(this.encoding);
- java.io.PrintWriter out = res.getWriter();
- out.print("过滤器设置编码为:"+this.encoding+"
"); chain.doFilter(req, res);
- }
- }
这样 每个页面的最前面都会出现一句话 过滤器设置编码为gb2312
阅读(1798) | 评论(0) | 转发(0) |