cookie.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="login.jsp"%>
<%
request.setCharacterEncoding("UTF-8");
String username = "";
int visitTimes = 0;
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());
}
}
if(username==null||username.trim().equals("")){
thrownew Exception("您还没有登录,请先登录");
}
//修改Cookie,更新用户的访问次数
Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(++visitTimes));
//覆盖名为visitTimes的Cookie
response.addCookie(visitTimesCookie);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</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="styles.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();"/></td></tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>
login.jsp页面
<%@ page language="java" import="java.util.*" 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>My JSP 'index.jsp' starting page</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="styles.css">
-->
</head>
<body>
<div align="center" style="margin:10px;">
<fieldset>
<legend>登录</legend>
<form action="login.jsp" method="post">
<table>
<tr><td></td><td><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="pwd" style="width:200px;"/></td></tr>
<tr><td></td><td align="center"><input type="submit" value="登录" style="width:100px;"/></td></tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>