Java中把Cookie封装成javax.servlet.http.Cookie类。每个Cookie都是该Cookie类的对象,服务器通过操作Cookie类对象对客户端Cookie进行操作。通过request.getCookie()获取客户端提交的所有Cookie,通过response.addCookie(Cookie cookie)向客户端设置Cookie。Cookie对象使用key-value属性的形式保存用户状态。
cookie.jsp
<%@ page language="java" pageEncoding="UTF-8" errorPage="login.jsp" %>
<%@ page import="javax.servlet.http.Cookie" %>
<%
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();
}
elseif("visitTimes".equals(cookie.getName())){
visitTimes = Integer.parseInt(cookie.getValue());
cookie.setValue("" + ++visitTimes);
}
}
// 如果没有找到 Cookie 中保存的用户名,则转到登录界面
if(username == null || username.trim().equals("")){
thrownew 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>
</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记录用户的访问次数。如果用户没有登录,则显示登录界面。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>请先登录</title>
</head>
<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>
配置文件
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>cookie.jsp</welcome-file>
</welcome-file-list>
</web-app>