zoukankan      html  css  js  c++  java
  • 用Filter解决乱码和jsp缓存问题

    1) 乱码Filter:

    新建一个:CharSetFilter

    package com.my.filter;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    
    public class CharSetFilter implements Filter {
        public CharSetFilter() {}
        
        public void init(FilterConfig config) throws ServletException {
        }
        
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse)resp;
            HttpServletRequest request = (HttpServletRequest)req;
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset="utf-8"");
            chain.doFilter(request,response);
        }
        
        public void destroy() {
        }
    }

    修改web.xml

        <filter>
            <filter-name>charsetFilter</filter-name>
            <filter-class>com.my.filter.CharSetFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>charsetFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    2) JSP缓存问题:

    新建一个:CacheFilter

    package com.my.filter;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    
    public class CacheFilter implements Filter {
        public CacheFilter() {}
        
        public void init(FilterConfig config) throws ServletException {
        }
        
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse)resp;
            HttpServletRequest request = (HttpServletRequest)req;
            response.setDateHeader("Expires",-1);
            response.setHeader("Cache-Control","no-cache");
            response.setHeader("Pragma","no-cache");
            chain.doFilter(request, response);
        }
        
        public void destroy() {
        }
    }

    修改web.xml

        <filter>
            <filter-name>cahceFilter</filter-name>
            <filter-class>com.my.filter.CacheFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>cahceFilter</filter-name>
            <url-pattern>*.jsp</url-pattern>
        </filter-mapping>
  • 相关阅读:
    Web开发中的服务器跳转与客户端跳转
    Linux设置程序开机自启动
    RedHat Linux6.4下安装apache服务
    FTP错误 [ftp: connect: No route to host] 解决方法
    虚拟机ping不通主机,但是主机可以ping通虚拟机(转载)
    工作中常用的Linux命令:mkdir命令
    工作中常用的Linux命令:crontab命令
    工作中常用的Linux命令:目录
    工作中常用的Linux命令:ipcs/ipcrm命令
    【CentOS】在Centos7 下无图形界面安装 Oracle11g
  • 原文地址:https://www.cnblogs.com/HD/p/3635425.html
Copyright © 2011-2022 走看看