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);