⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encodingfilter.java

📁 分页类,包括中文和英文及其他语言种类,便于二次开发及商业开发,
💻 JAVA
字号:
package com.laoer.comm.web;

import javax.servlet.http.HttpServlet;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * <p>Title: 天乙软件工作室公共包</p>
 * <p>Description: 天乙软件工作室公共包</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 天乙软件工作室[LAOER.COM/TIANYISOFT.NET]</p>
 * @author 龚天乙(Laoer)
 * @version 1.0
 */

public class EncodingFilter
    extends HttpServlet
    implements Filter {

  private FilterConfig filterConfig;
  private String targetEncoding = "ASCII";

  /**
   * Called by the web container to indicate to a filter that it is being placed
   * into service.
   *
   * @param filterConfig FilterConfig
   * @throws ServletException
   * @todo Implement this javax.servlet.Filter method
   */
  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    this.targetEncoding = filterConfig.getInitParameter("encoding");
  }

  /**
   * The <code>doFilter</code> method of the Filter is called by the container
   * each time a request/response pair is passed through the chain due to a
   * client request for a resource at the end of the chain.
   *
   * @param request ServletRequest
   * @param response ServletResponse
   * @param chain FilterChain
   * @throws IOException
   * @throws ServletException
   * @todo Implement this javax.servlet.Filter method
   */
  public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                       FilterChain chain) throws IOException, ServletException {
    try {
      HttpServletRequest request = (HttpServletRequest) srequest;
      request.setCharacterEncoding(targetEncoding);
      chain.doFilter(srequest, sresponse);
    }
    catch (ServletException sx) {
      filterConfig.getServletContext().log(sx.getMessage());
    }
    catch (IOException iox) {
      filterConfig.getServletContext().log(iox.getMessage());
    }
  }

  /**
   * Called by the web container to indicate to a filter that it is being taken
   * out of service.
   *
   * @todo Implement this javax.servlet.Filter method
   */
  public void destroy() {
    filterConfig = null;
    targetEncoding = null;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -