您的位置:首页 > 编程语言 > Java开发

Java中使用Cookie记录用户访问次数

2016-02-11 04:19 513 查看
Java中把Cookie封装成了javax.servlet.http.Cookie类。每个Cookie都应该是该Cookie类的对象。服务器通过操作Cookie类对象对客户端Cookie进行操作。通过request.Cookie()获得客户端提交的所有Cookie(以Cookie[]数组形式返回),通过response.addCookie(Cookie cookie)向客户端设置Cookie.

Cookie对象使用key-value属性对的形式保存用户状态,一个Cookie对象保存一个属性对,一个request或者response同时使用多个Cookie.因为Cookie类位于包javax.servlet.http.*下面,所有JSP不需要import该类。

看一个使用Cookie记录用户账号以及登录次数的例子。新建JSP页面cookie.jsp,输入如下源代码:

cookie.jsp

<%@ page language="java" pageEncoding="UTF-8" errorPage="login.jsp" %>
<%
request.setCharacterEncoding("UTF-8");

String username = "";
int visitTimes = 0;

// 所有的 cookie
Cookie[] cookies = request.getCookies();

// 遍历所有的 Cookie 寻找 用户帐号信息与登录次数信息
for(int i=0; cookies!=null&&i<cookies.length; i++){
Cookie cookie = cookies[i];
if("username".equals(cookie.getName())){
username = cookie.getValue();
}
else if("visitTimes".equals(cookie.getName())){
visitTimes = Integer.parseInt(cookie.getValue());
cookie.setValue("" + ++visitTimes);
}
}

// 如果没有找到 Cookie 中保存的用户名,则转到登录界面
if(username == null || username.trim().equals("")){
throw new Exception("您还没有登录。请先登录");
}

// 修改 Cookie,更新用户的访问次数
Cookie visitTimesCookie = new Cookie("visitTimes", Integer.toString(++visitTimes));
response.addCookie(visitTimesCookie);

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Cookie</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div align="center" style="margin:10px; ">
<fieldset>
<legend>登录信息</legend>
<form action="login.jsp" method="post">
<table>
<tr>
<td>
您的帐号:
</td>
<td>
<%= username %>
</td>
</tr>
<tr>
<td>
登录次数:
</td>
<td>
<%= visitTimes %>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value=" 刷  新 "
onclick="location='<%= request.getRequestURI() %>?ts=' + new Date().getTime(); "
class="button">
</td>
</tr>
</table>
</form>
</fieldset>
</div>

</body>
</html>


程序使用Cookie记录用户的访问次数。如果用户没有登录,则显示登录界面。工作原理是程序先检查Cookie,如果没有找到包含username属性的Cookie,则抛出异常,页面转跳到errorPage指定的错误处理页面login.jsp.login.jsp的源代码如下:

login.jsp

<%@ page language="java" pageEncoding="UTF-8" isErrorPage="true" %>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

if("POST".equals(request.getMethod())){

Cookie usernameCookie = new Cookie("username", request.getParameter("username"));
Cookie visittimesCookie = new Cookie("visitTimes", "0");

response.addCookie(usernameCookie);
response.addCookie(visittimesCookie);

response.sendRedirect(request.getContextPath() + "/cookie.jsp");

return;
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>请先登录</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div align="center" style="margin:10px; ">
<fieldset>
<legend>登录</legend>
<form action="login.jsp" method="post">
<table>
<tr>
<td>
</td>
<td>
<span><img src="images/errorstate.gif"></span>
<span style="color:red; "><%= exception.getMessage() %></span>
</td>
</tr>
<tr>
<td>
帐号:
</td>
<td>
<input type="text" name="username" style="width:200px; ">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password" style="width:200px; ">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value=" 登  录 " class="button">
</td>
</tr>
</table>
</form>
</fieldset>
</div>

</body>
</html>


程序运行效果如图所示:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: