• 文件过滤器


    package com.itheima.web.filters;

    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.annotation.WebInitParam;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * 字符集过滤器
    * @author 黑马程序员
    * @Company http://www.itheima.com
    */
    @WebFilter(value = "/*",initParams={@WebInitParam(name = "encoding",value = "UTF-8")})
    public class CharacterEncodingFilter implements Filter {

    private FilterConfig filterConfig;

    /**
    * 初始化方法,获取过滤器的配置对象
    * @param filterConfig
    * @throws ServletException
    */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    //1.定义和协议相关的请求和响应对象
    HttpServletRequest request ;
    HttpServletResponse response;
    try{
    //2.把参数转换成协议相关的对象
    request = (HttpServletRequest)req;
    response = (HttpServletResponse)resp;
    //3.获取配置的字符集
    String encoding = filterConfig.getInitParameter("encoding");
    //4.设置请求参数的字符集
    request.setCharacterEncoding(encoding);
    //5.设置响应对象输出响应正文时的字符集
    response.setContentType("text/html;charset=UTF-8");
    //6.放行
    chain.doFilter(request,response);
    }catch (Exception e){
    e.printStackTrace();
    }
    }

    @Override
    public void destroy() {
    //可以做一些清理操作
    }
    }
  • 相关阅读:
    代码注释技术
    疑难杂症错误解决方法大全
    MD5 加密
    ADO.NET DataReader和DataAdapter的区别
    HTTP协议详解
    web开发常用样式
    Stream 和 byte[] 之间的转换
    Sql 函数大全 (更新中...由难到简
    Web C# 导出Excel 方法总结
    VC++ MFC 如何实现在编辑框中输出具有换行功能的文段 01
  • 原文地址:https://www.cnblogs.com/KingAndPig/p/13732553.html
Copyright © 2020-2023  润新知