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">

这个标准
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值