设计一个jsp页面使用cookie技术识别用户是否访问过本网页(用客户端IP地址识别用户) 15
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%@pageimport="java.n...
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="java.net.URLDecoder" %><%@ page import="java.util.Date" %><html><head><meta charset="UTF-8"><title>读取cookie信息</title></head><body><%String date="";String userIP="";Cookie[] cookies = request.getCookies();if(cookies!=null) { for(int i=0; i<cookies.length; i++) { if(cookies[i].getName().equals("mrCookie")){ userIP=cookies[i].getValue().split("#")[0]; date=cookies[i].getValue().split("#")[1]; } } }if("".equals(userIP)){%><center><p>尊敬的用户,欢迎您首次访问本网站!</p><%userIP=request.getRemoteAddr();%><br>使用的IP地址是:<%=userIP%><p>您本次访问本网站的时间是:<br><%Date d=new Date();out.print(d); %></p></center><%}else{ %> <center> <p>尊敬的用户,欢迎您再次访问本网站!</p> <br>您上次访问本网站的时间是: <%=date %> <br>使用的IP地址是: <%=userIP %> </center><% }%></body></html> <%@page import="java.text.SimpleDateFormat"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="java.net.URLDecoder" %><%@ page import="javax.servlet.http.Cookie" %><html><head><title>写cookie信息</title></head><body><%request.setCharacterEncoding("UTF-8");String userIP=request.getRemoteAddr();Date d=new Date();String date=d.toString();Cookie cookie=new Cookie("mrCookie",userIP+"#"+date);cookie.setMaxAge(60*2);cookie.setValue(userIP);userIP=request.getRemoteAddr();response.addCookie(cookie);%><script type="text/javascript">window.location.href="cookie.jsp"</script></body></html>目前只能显示初次访问页面,而不能显示再次访问的页面错误在图片显示(如果图片不清楚:HTTP Status 500 – Internal Server ErrorType Exception ReportMessage An exception occurred processing [cookie.jsp] at line [18]Description The server encountered an unexpected condition that prevented it from fulfilling the request.Exceptionorg.apache.jasper.JasperException: An exception occurred processing [cookie.jsp] at line [18]15: for(int i=0; i<cookies.length; i++) {16: if(cookies[i].getName().equals("mrCookie")){17: userIP=cookies[i].getValue().split("#")[0];18: date=cookies[i].getValue().split("#")[1];19: }20: } 21: }
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询