• filter 静态资源


    package com.itheima.web.filter;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * 静态资源要缓存
     * @author wangli
     *
     */
    
    public class StaticSourceNeededCacheFilter3 implements Filter {
        
        private FilterConfig config ;
        public void destroy() {
            
        }
    
        
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            //1.得到访问资源的类型-------------->就看扩展名--------------->怎么取扩展名????
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
            
            //2.取到你访问的资源,再取出扩展名
            String uri = request.getRequestURI();//取到你访问的资源   /fdsfds/fsdfdsf/aa.html
            String extendsionName = uri.substring(uri.lastIndexOf(".")+1);//得到扩展名
            
            //3.得到这个资源的缓存时间
            String time="0";
            time = config.getInitParameter(extendsionName);
            
            //4.设置时期时间
            if(time!=null){
                response.setDateHeader("Expires",System.currentTimeMillis()+ Integer.parseInt(time)*60*60*1000);//单位:毫秒
            }
            chain.doFilter(request, response);
        }
    
        
        public void init(FilterConfig fConfig) throws ServletException {
            this.config = fConfig;
        }
    
    }

    配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
      <display-name></display-name>
      <servlet>
        <servlet-name>CustomerRegistServlet</servlet-name>
        <servlet-class>com.itheima.web.servlet.CustomerRegistServlet</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>UserRegistServlet</servlet-name>
        <servlet-class>com.itheima.web.servlet.UserRegistServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>CustomerRegistServlet</servlet-name>
        <url-pattern>/servlet/CustomerRegistServlet</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>UserRegistServlet</servlet-name>
        <url-pattern>/servlet/UserRegistServlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <filter>
        <display-name>CharacterEncodingFilter</display-name>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.itheima.web.filter.CharacterEncodingFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <!-- <dispatcher>REQUEST</dispatcher>  默认类型就是REQUEST -->
      </filter-mapping>
     <!--  <filter>
        <display-name>DynamicSourceNotNeededCacheFilter</display-name>
        <filter-name>DynamicSourceNotNeededCacheFilter</filter-name>
        <filter-class>com.itheima.web.filter.DynamicSourceNotNeededCacheFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>DynamicSourceNotNeededCacheFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping> -->
      <filter>
        <display-name>StaticSourceNeededCacheFilter3</display-name>
        <filter-name>StaticSourceNeededCacheFilter3</filter-name>
        <filter-class>com.itheima.web.filter.StaticSourceNeededCacheFilter3</filter-class>
        <init-param>
          <param-name>js</param-name>
          <param-value>1</param-value><!-- hour -->
        </init-param>
        <init-param>
          <param-name>css</param-name>
          <param-value>2</param-value>
        </init-param>
        <init-param>
          <param-name>html</param-name>
          <param-value>3</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>StaticSourceNeededCacheFilter3</filter-name>
        <url-pattern>*.js</url-pattern>
      </filter-mapping>
      <filter-mapping>
        <filter-name>StaticSourceNeededCacheFilter3</filter-name>
        <url-pattern>*.css</url-pattern>
      </filter-mapping>
      <filter-mapping>
        <filter-name>StaticSourceNeededCacheFilter3</filter-name>
        <url-pattern>*.html</url-pattern>
      </filter-mapping>
    </web-app>
  • 相关阅读:
    Java回调机制
    显示Title和隐藏Title的ListView
    ListView的小知识
    小知识点
    Shader的使用
    项目知识(二)
    项目知识(一)
    复习篇(一)Activity的生命周期和启动模式
    BootStrap 提示框
    BootStrap选项卡
  • 原文地址:https://www.cnblogs.com/baijin05/p/5086405.html
Copyright © 2020-2023  润新知