投票过滤器

IP归属
public static long getIpNum(String ip){
long ipNum=0;
if(ip!=null&&!ip.equals("")){
String[] subips=ip.split("\\.");
for(int i=0;i<subips.length;i++){
ipNum+=Integer.parseInt(subips[i]);
if(i<subips.length-1)
ipNum=ipNum<<8;
}
}
return ipNum;
}


//记录使用该IP的用户的投票时间
public static String timeTostr(Date date){
String strDate="";
if(date!=null){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
strDate=format.format(date);
}
return strDate;
}


投票过滤器类
public class VoteLimitFilter implements Filter {
private FilterConfig fc=null;

public void doFilter(ServletRequest srequest, ServletResponse sresponse,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)srequest;
HttpServletResponse response=(HttpServletResponse)sresponse;
HttpSession session=request.getSession();

//查询服务器端该IP上次投票的时间
String ip=request.getRemoteAddr(); //获取客户端IP
long ipnum=StringHandler.getIpNum(ip);
int optionid=Integer.parseInt(request.getParameter("movie")); //获取选择的选项ID
try {
VoterDao voterDao=new VoterDao();
Date now=new Date(); //获取当前时间
Date last = voterDao.getLastVoteTime(ipnum); //获取该IP的上次投票时间

if(last==null){ //数据库中没有记录该IP,则该IP地址没有投过票
addCookie(request,response); //在客户端的cookie中记录该用户已经投过票

Object[] params={ipnum,optionid,StringHandler.timeTostr(now)};
voterDao.saveVoteTime(params); //在数据库中记录该IP、选择的选项ID和投票时间
chain.doFilter(request,response);
}
else{ //该IP地址投过票,则接着判断客户端cookie中是否记录了用户投票情况(用来解决局域网中某个ip投票后,其他ip不能再进行投票的问题)
boolean voteincookie=seeCookie(request); //判断当前使用该IP的用户的客户端的cookie中是否记录了投票标记
if(voteincookie){ //如果记录了该用户已经投过票
request.setAttribute("message","● 您已经投过票了,1小时内不允许重复投票!");
RequestDispatcher rd=request.getRequestDispatcher("fail.jsp");
rd.forward(request,response);
}
else{ //没有记录该用户是否投过票,则接着判断当前session中是否记录了用户投票的情况(用来解决用户投票后,删除本地cookie实现重复投票)
String ido=(String)session.getAttribute("ido");
if("yes".equals(ido)){ //当前用户已投过票
request.setAttribute("message","● 您已经投过票了,1小时内不允许重复投票!");
RequestDispatcher rd=request.getRequestDispatcher("fail.jsp");
rd.forward(request,response);
}
else{
addCookie(request,response); //在客户端的cookie中记录该用户已经投过票

Object[] params={ipnum,optionid,StringHandler.timeTostr(now)};
voterDao.saveVoteTime(params); //记录使用该IP的用户的投票时间
chain.doFilter(request,response);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}


private boolean seeCookie(HttpServletRequest request){
boolean hasvote=false;
String webName=request.getContextPath();
webName=webName.substring(1);
String cookiename=webName+".voter";

Cookie[] cookies=request.getCookies();
if(cookies!=null&&cookies.length!=0){
for(int i=0;i<cookies.length;i++){
Cookie single=cookies[i];
if(single.getName().equals(cookiename)&&single.getValue().equals("I Have Vote")){
hasvote=true;
break;
}
}
}
return hasvote;
}

//在客户端的cookie中记录该用户已经投过票
private void addCookie(HttpServletRequest request,HttpServletResponse response){
String webname=request.getContextPath();
webname=webname.substring(1);
Cookie cookie=new Cookie(webname+".voter","I Have Vote"); //创建一个cookie
cookie.setPath("/");
cookie.setMaxAge(60*60*1); //设置cookie在客户端保存的有效时间为1小时
response.addCookie(cookie); //向客户端写入cookie
}
public void init(FilterConfig fc) throws ServletException {
this.fc=fc;
}
public void destroy() {
this.fc=null;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值