package com.hrs.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: EncodingFilter.java
* @Description: 字符编码过滤器
*
* @version: v1.0.0
* @author: 赵恒
* @date: 2026年4月13日 上午9:36:34 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     赵恒           v1.0.0               新建文件
*/
@WebFilter("/*")
public class EncodingFilter implements Filter {
    
    private String encoding = "UTF-8";
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String param = filterConfig.getInitParameter("encoding");
        if (param != null && !param.isEmpty()) {
            encoding = param;
        }
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 设置请求编码
        request.setCharacterEncoding(encoding);
        // 设置响应编码
        response.setCharacterEncoding(encoding);
        // 继续执行
        chain.doFilter(request, response);
    }
    
    @Override
    public void destroy() {
    }
}