package com.tools;
/* * 转码(过期时间限制)
* @author hh
* @2010-09-16
*/
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
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;
@SuppressWarnings("serial")
public class filter implements Filter {
private String CEncoding="";
public void destroy() {
}
@SuppressWarnings("static-access")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2) throws IOException, ServletException {
Calendar c = Calendar.getInstance();
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
req.setCharacterEncoding(CEncoding);
res.setCharacterEncoding(CEncoding);
String nowDate="2011-03-25";//开始时间
long flage = betweenTwoDay(nowDate, new SimpleDateFormat("yyyy-MM-dd").format(c.getInstance().getTime()));
if(flage>=0&&flage<=30){
arg2.doFilter(req, res);
}else{
req.getRequestDispatcher("/TimeOut.jsp").forward(req,res);//提示界面
}
}
private long betweenTwoDay(String first, String second) {
Date fdate = getStringToDate(first);
Date sdate = getStringToDate(second);
long i = ((sdate.getTime() - fdate.getTime()) / (24 * 60 * 60 * 1000));
return i;
}
private String format = "";
private Date getStringToDate(String date) {
Date d = null;
try {
SimpleDateFormat dateFormat = null;
if (date.length() == 10) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
format = "yyyy-MM-dd";
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
} else if (date.length() == 16) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
format = "yyyy-MM-dd HH:mm";
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
} else if (date.length() == 19) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = "yyyy-MM-dd HH:mm:ss";
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
}
d = dateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public void init(FilterConfig arg0) throws ServletException {
//读取web.xml文件设置编码格式
CEncoding = arg0.getInitParameter("encoding");
}
}