2. 代码记录

全站编码过滤器

public class EncodingFilter implements Filter {
    private String encoding;
//    private String serverGetEncoding;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");
//        this.serverGetEncoding = filterConfig.getInitParameter("serverGetEncoding");

        if (this.encoding == null || "".equals(this.encoding)) {
            this.encoding = "UTF-8";
        }

//        if (this.serverGetEncoding == null || "".equals(this.serverGetEncoding)) {
//            this.serverGetEncoding = "ISO-8859-1";
//        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
    		throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        request.setCharacterEncoding(encoding);
        response.setContentType("text/html; charset=" + encoding);

        // 当Tomcat 版本在8.0 以下时,收到GET 请求方式的url 链接,服务器会以ISO-8859-1 对url 进行编码,如果是post 请求,
        // 一般页面设置了请求的编码类型为UTF-8 ,故服务器会按照UTF-8 编码。所以,这时需要单独对GET请求方式的request 重写
        // getParameter() 方法。
        
//        MyRequest myRequest = new MyRequest(request, encoding, serverGetEncoding);

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}


// MyRequest 工具类,Tomcat8.0 以上就不需要重写GET 请求的getParameter() 方法,服务器内部已经自动修正。
public class MyRequest extends HttpServletRequestWrapper {
    private String encoding;
    private String serverGetEncoding;
    private Map<String, String> customHeaders;

    public MyRequest(HttpServletRequest request) {
        super(request);
        this.customHeaders = new HashMap<>();
    }

    public MyRequest(HttpServletRequest request, String encoding, String serverGetEncoding) {
        super(request);
        this.encoding = encoding;
        this.serverGetEncoding = serverGetEncoding;
    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);

        if(super.getMethod().equalsIgnoreCase("GET")){
            try {
                value = new String(value.getBytes(serverGetEncoding), encoding);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return value;
    }

    public void addHeader(String name, String value) {
        this.customHeaders.put(name, value);
    }

    @Override
    public String getHeader(String name) {
        String value = this.customHeaders.get(name);

        if (value != null) {
            return value;
        }

        return ((HttpServletRequest) getRequest()).getHeader(name);
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        Set<String> set = new HashSet<>(customHeaders.keySet());
        Enumeration<String> enumeration = ((HttpServletRequest) getRequest()).getHeaderNames();

        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            set.add(name);
        }

        return Collections.enumeration(set);
    }
}

// web.xml 配置。
  <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>priv.technologyshareweb.filter.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

自定义 SessionContext

public class MySessionContext {
    private static HashMap<String, HttpSession> myMap = new HashMap<String, HttpSession>();

    public static synchronized void addSession(HttpSession session) {
        if (session != null) {
            myMap.put(session.getId(), session);
        }
    }

    public static synchronized void delSession(HttpSession session) {
        if (session != null) {
            myMap.remove(session.getId());
        }
    }

    public static synchronized HttpSession getSession(String sessionId) {
        if (sessionId == null) {
            return null;
        }

        return myMap.get(sessionId);
    }
}

// Session 监听器。
@WebListener
public class MySessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent)  {
        HttpSession session = httpSessionEvent.getSession();

        MySessionContext.addSession(session);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent)  {
        HttpSession session = httpSessionEvent.getSession();

        MySessionContext.delSession(session);
    }
}

// web.xml 可加配置。
  <listener>
    <listener-class>priv.technologyshareweb.listener.MySessionListener</listener-class>
  </listener>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值