首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

Filter兑现动态更换请求链接(css和swf),黑白功能实现

2012-12-23 
Filter实现动态更换请求链接(css和swf),黑白功能实现Filter代码public void doFilter(ServletRequest req,

Filter实现动态更换请求链接(css和swf),黑白功能实现
Filter代码

public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
  HttpServletRequest request = (HttpServletRequest) req;
  HttpServletResponse response = (HttpServletResponse) res;
  ServletContext application = request.getSession().getServletContext();
  Object b = application.getAttribute("whiteblackflag");
  if (b != null) {
   boolean flag = (Boolean) b;
   if (flag) { // 要黑白
    Timestamp startDate = (Timestamp) application
      .getAttribute("startDate");
    Timestamp endDate = (Timestamp) application
      .getAttribute("endDate");
    Timestamp now = new Timestamp(new Date().getTime());
    long startDateNum = startDate.getTime();
    long nowNum = now.getTime();
    // System.out.println("开始时间大小:"+startDateNum);
    // System.out.println("现在时间大小:"+nowNum);
    if (endDate != null) { // 有结束时间
     long endDateNum = endDate.getTime();
     // System.out.println("结束时间大小:"+endDateNum);
     if (nowNum >= startDateNum && nowNum <= endDateNum) {
      String url = request.getServletPath();
      // System.out.println(url);
      if (url.indexOf("color.css") != -1) {
       url = url.replaceAll("color.css", "color_grey.css");
       RequestDispatcher go = request
         .getRequestDispatcher(url);
       go.forward(request, response);
      }
      if (url.indexOf(".swf") != -1) {
       // System.out.println("有swf的:"+url);
       url = url.replaceAll(".swf", "_grey.swf");
       // System.out.println("有swf的变化后:"+url);
       RequestDispatcher go = request
         .getRequestDispatcher(url);
       go.forward(request, response);
      }
     }
    } else { // 无结束时间
    // System.out.println("无结束时间。。");
     if (nowNum >= startDateNum) { // 无结束时间,且现在时间大于开始时间才执行
      String url = request.getServletPath();
      System.out.println(url);
      if (url.indexOf("color.css") != -1) {
       url = url.replaceAll("color.css", "color_grey.css");
       RequestDispatcher go = request
         .getRequestDispatcher(url);
       go.forward(request, response);
      }
      if (url.indexOf(".swf") != -1) {
       // System.out.println("有swf的:"+url);
       url = url.replaceAll(".swf", "_grey.swf");
       // System.out.println("有swf的变化后:"+url);
       RequestDispatcher go = request
         .getRequestDispatcher(url);
       go.forward(request, response);
      }
     }
    }
   }
  } else {
   System.out.println("标记b为null");
  }
  chain.doFilter(request, response);
}



web.xml表中配置

<filter>
  <filter-name>GreyFilter</filter-name>
  <filter-class>com.gdie.gdsafety.webapp.filter.GreyFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>GreyFilter</filter-name>
  <url-pattern>/images/color.css</url-pattern>
</filter-mapping>
  <filter-mapping>
  <filter-name>GreyFilter</filter-name>
  <url-pattern>*.swf</url-pattern>
</filter-mapping>



实现黑白效果功能:

其中color.css是空的,而过滤成color_grey.css也只有一句代码:

html{filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);}

这句话放在css最顶部就可以实现黑白效果.

color.css是在页面中所有css的最开始部分才能实现,且页面要遵守

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

这个标准

热点排行