CharacterEncodingFilter.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:3k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.mycompany.filter;
  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. public class CharacterEncodingFilter implements Filter {
  10.     /**
  11.      * The default character encoding to set for requests that pass through
  12.      * this filter.
  13.      */
  14.     protected String encoding = null;
  15.     /**
  16.      * The filter configuration object we are associated with.  If this value
  17.      * is null, this filter instance is not currently configured.
  18.      */
  19.     protected FilterConfig filterConfig = null;
  20.     /**
  21.      * Should a character encoding specified by the client be ignored?
  22.      */
  23.     protected boolean ignore = true;
  24.     
  25.     /**
  26.      * Place this filter into service.
  27.      *
  28.      * @param filterConfig The filter configuration object
  29.      */
  30.     public void init(FilterConfig filterConfig) throws ServletException {
  31.         this.filterConfig = filterConfig;
  32.         this.encoding = filterConfig.getInitParameter("encoding");
  33.         String value = filterConfig.getInitParameter("ignore");
  34.         if (value == null) {
  35.             this.ignore = true;
  36.         } else if (value.equalsIgnoreCase("true")) {
  37.             this.ignore = true;
  38.         } else {
  39.             this.ignore = false;
  40.         }
  41.     }
  42.     
  43.     /**
  44.      * Take this filter out of service.
  45.      */
  46.     public void destroy() {
  47.         this.encoding = null;
  48.         this.filterConfig = null;
  49.     }
  50.     /**
  51.      * Select and set (if specified) the character encoding to be used to
  52.      * interpret request parameters for this request.
  53.      *
  54.      * @param request The servlet request we are processing
  55.      * @param result The servlet response we are creating
  56.      * @param chain The filter chain we are processing
  57.      *
  58.      * @exception IOException if an input/output error occurs
  59.      * @exception ServletException if a servlet error occurs
  60.      */
  61.     public void doFilter(ServletRequest request, ServletResponse response,
  62.                          FilterChain chain)
  63. throws IOException, ServletException {
  64.         // Conditionally select and set the character encoding to be used
  65.         if (ignore || (request.getCharacterEncoding() == null)) {
  66.             String encoding = selectEncoding(request);
  67.             if (encoding != null) {
  68.                 request.setCharacterEncoding(encoding);
  69.             }
  70.         }
  71.         // Pass control on to the next filter
  72.         chain.doFilter(request, response);
  73.     }
  74.     /**
  75.      * Select an appropriate character encoding to be used, based on the
  76.      * characteristics of the current request and/or filter initialization
  77.      * parameters.  If no character encoding should be set, return
  78.      * <code>null</code>.
  79.      * <p>
  80.      * The default implementation unconditionally returns the value configured
  81.      * by the <strong>encoding</strong> initialization parameter for this
  82.      * filter.
  83.      *
  84.      * @param request The servlet request we are processing
  85.      */
  86.     protected String selectEncoding(ServletRequest request) {
  87.         return this.encoding;
  88.     }
  89. }