日期:2014-05-17  浏览次数:20641 次

jsp 设置缓存 图片缓存 js缓存 css缓存
  
package com.elink.control;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.elink.util.*;

/**
 * @author liubj
 */

public class BusiCacheFilter implements Filter
{
 private FilterConfig config  = null;
 private HashMap expiresMap = new HashMap();

 public void init(FilterConfig filterConfig)
 {
  this.config = filterConfig;
  expiresMap.clear();
  Enumeration names = config.getInitParameterNames();
  while( names.hasMoreElements() )
  {
   try
   {
    String name = (String)names.nextElement();
    String value = config.getInitParameter( name );
    Integer expire = Integer.valueOf( value );
    expiresMap.put( name, expire );
   }
   catch( Exception ex)
   {
   }
  }
 }

 public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException
 {
  HttpServletRequest req = (HttpServletRequest) request;
  HttpServletResponse res = (HttpServletResponse) response;
  
  String uri = req.getRequestURI();
  String ext = null;
  int dot =  uri.lastIndexOf(".");
  if( dot != -1 )
  {
   ext = uri.substring( dot+1 );
  }
  
  setResponseHeader( res, uri, ext );
  chain.doFilter(request, response);
 }

 public void destroy()
 {
 }
 
 protected FilterConfig getFilterConfig()
 {
  return (config);
 }
 
 
 private void setResponseHeader( HttpServletResponse response, String uri, String ext )
 {
  if( ext!= null && ext.length() > 0 )
  {
   Integer expires = (Integer)expiresMap.get(ext);
   if( expires != null )
   {
    LogUtil.logDebug( uri + ".Expires: "+ expires.intValue());
    if( expires.intValue() > 0 )
    {
     response.setHeader("Cache-Control","max-age="+expires.intValue()); //HTTP 1.1
    }
    else
    {
     response.setHeader("Cache-Control","no-cache");
     response.setHeader("Pragma","no-cache"); //HTTP 1.0
     response.setDateHeader ("Expires", 0 );
    }
   }
  }
 }
}

?

web.xml

?

<filter>
 <filter-name>busicachefilter</filter-name>
 <filter-class>com.elink.control.BusiCacheFilter</filter-class>
 <init-param>
  <param-name>js</param-name>
  <param-value>3600</param-value>
 </init-param>
 <init-param>
  <param-name>gif</param-name>
  <param-value>3600</param-value>
 </init-param>
 <init-param>
  <param-name>jpg</param-name>
  <param-value>3600</param-value>
 </init-param>
 <init-param>
  <param-name>css</param-name>
  <param-value>3600</param-value>
 </init-param>
  </filter>
  <filter-mapping>
 <filter-name>busicachefilter</filter-name>
 <url-pattern>*.js</url-pattern>
  </filter-mapping>
  <filter-mapping>
 <filter-name>busicachefilter</filter-name>
 <url-pattern>*.gif</url-pattern>
  </filter-mapping>
  <filter-mapping>
 <filter-name>busicachefilter</filter-name>
 <url-pattern>*.jpg</url-pattern>
  </filter-mapping>
  <filter-mapping>
 <filter-name>busicachefilter</filter-name>
 <url-pattern>*.css</url-pattern>
  </filter-mapping>

?
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/netcom19/archive/2004/12/11/213141.aspx