程序使用Cookie记录用户的访问次数。如果用户没有登录,则显示登录界面。工作原理是程序先查询Cookie,如果没有找到包含username助兴的Cookie,则抛出异常,页面跳转到errorPage指定的错误处理页面login.jsp。
cookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="login.jsp"%>
<%
request.setCharacterEncoding("UTF-8");//设置request编码
String username ="";//用户名
int visitTimes = 0;//访问次数
Cookie[] cookies =request.getCookies();
for(int i = 0;cookies != null && i < cookies.length;i++){//便利Cookie寻找照顾好与登录次数
Cookie cookie = cookies[i];
if("username".equals(cookie.getName())){
username = cookie.getValue();
}else if("visitTimes".equals(cookie.getName())){
visitTimes = Integer.parseInt(cookie.getValue());
}
}
if(username == null || username.trim().equals("")){//如果没有找到用户名,则转到登录界面
throw new Exception("您还没有登录,请先登录");
}
//修改Cookie,更新用户的访问次数
Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(++visitTimes));
response.addCookie(visitTimesCookie);//覆盖名为visitTimes的Cookie
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</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="刷新" οnclick="location='<%=request.getRequestURI() %>?ts='+new Date().getTime();" class="button">
</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())){//如果以post方式登录
Cookie usernameCookie = new Cookie("username",request.getParameter("username"));
Cookie visitTimesCookie = new Cookie("visitTimes","0");//新建Cookie
response.addCookie(usernameCookie);//添加到response中
response.addCookie(visitTimesCookie);//response会将Cookie发送到客户端
response.sendRedirect(request.getContextPath()+"/cookie.jsp");//显示cookie界面
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">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</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/errorstae.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>