日期:2014-05-17  浏览次数:21068 次

关于投票,第一次报错,以后可以正常运行的问题???

每次都是在第一次点击投票的时候,弹出一个js的对话框,上面写着error,但是第二次点击是就可以投票,我限制的是,每天只能投票5次。。。在网上查了下,说是数组下标越界

错误:

警告: Method execution failed: 
java.lang.ArrayIndexOutOfBoundsException: 1
at com.jy.action.UserUpdatepiaoshu.updates(UserUpdatepiaoshu.java:133) .....
警告: --Erroring: batchId[0] message[java.lang.ArrayIndexOutOfBoundsException: 1]

【报错信息说是133行,133行就是

if(!"".equals(cookie_ip_array[1].toString().trim()) && Integer.parseInt(cookie_ip_array[1])>=6)



如果将cookie_ip_array[1]改为cookie_ip_array[0]那么如果用户禁用cookie后,就可以不限制的刷票,
如果还是cookie_ip_array[1]的话,第一次进行投票的时候,会报error错误,但是用户禁用cookie后,在投票的话,也会报error,不会刷票,求高手解答???????????


//获取cookie
public String getCookieIPAddress(HttpServletRequest request,HttpServletResponse response,String ip_address)
{  
Cookie[] cookies = request.getCookies();  
String cookievalue ="";  
 
if(null!=cookies && cookies.length!=0)
{  
for(int i=0;i<cookies.length;i++)
{  
Cookie cookie = cookies[i];  
if(cookie.getName().equals("cookie_ip_"+ip_address))
{  
String value= cookie.getValue();  
String[] value_array=value.split("\\|");  
int time= Integer.parseInt(value_array[1])+1;  
cookievalue = value_array[0]+"|"+time;//取出原来cookie中的信息,在后面追加信息  
Cookie c = new Cookie("cookie_ip_"+ip_address,cookievalue); //编码后添加都cookie  
c.setMaxAge(60 * 60);  
c.setPath("/");  
response.addCookie(c);  
}  
else{  
Cookie c = new Cookie("cookie_ip_"+ip_address,ip_address+"|1");  
c.setMaxAge(24*60*60);  
c.setPath("/");  
response.addCookie(c);  
}  
System.out.println(cookievalue);
}  
}  
 
return cookievalue;  
}  


//获取ip
public String getIpAddr(HttpServletRequest request){ 

String ip = request.getHeader("x-forwarded-for");  

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
{  
ip = request.getHeader("Proxy-Client-IP");  
}  
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("WL-Proxy-Client-IP");  
}  
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{  
ip = request.getRemoteAddr();  
}  
System.out.println(ip);
return ip;  



//修改票数
public int updates(HttpServletRequest request,HttpServletResponse response,int id){

//String id = request.getParameter("article_id")!=null ? request.getParameter("id").toString() : "";  

String ip_address = getIpAddr(request); 
String cookie_ip = getCookieIPAddress(request, response, ip_address);
Stri