jsp根据Cookie判断用户是否登录实例



jsp根据Cookie判断用户是否登录实例,jsp获取客户端Cookie的方法是什么?修改 Cookie内容,更新用户的访问次数,设置request编码,解决request请求中的中文乱码等:

cookie.jsp源码实例:

<%@ page language=”java” pageEncoding=”utf-8″ errorPage=”error.jsp”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘cookie.jsp’ starting page</title>
</head>
<%
// 设置request编码,解决request请求中的中文乱码
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++){
// 第i个Cookie信息
Cookie cookie = cookies[i];
// 如果获得的Cookie名为username
if(cookie.getName().equals(“username”)){
// 则记录该Cookie对应的内容
username = cookie.getValue();
}
// 如果获得的Cookie名为visitTimes
else if(cookie.getName().equals(“visitTimes”)){
// 则记录该Cookie对应的内容
visitTimes = Integer.parseInt(cookie.getValue());
}
}
// 如果没有找到用户名,则转到错误页面error.jsp。
if(username == null || username.trim().equals(“”)){
//在页面显示为大陆的相关信息
throw new Exception(“您还没有登录。请先登录”);
}
// 修改 Cookie内容,更新用户的访问次数
Cookie visitTimesCookie = new Cookie(“visitTimes”, Integer.toString(++visitTimes));
// 覆盖名为visitTimes的Cookie
response.addCookie(visitTimesCookie);
%>

<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>

error.jsp页面源码:

<%@ page language=”java” pageEncoding=”utf-8″ isErrorPage=”true” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>请先登录</title>
</head>
<%
// 设置request编码方式,解决request请求中的中文乱码
request.setCharacterEncoding(“UTF-8″);
// 设置response编码方式,解决response响应中的中文乱码
response.setCharacterEncoding(“UTF-8″);
// 判断,如果是以POST方式提交请求
if(request.getMethod().equals(“POST”)){
// 新建名为username的Cookie对象,原来记录登陆的用户名
Cookie usernameCookie =
new Cookie(“username”, request.getParameter(“username”));
// 新建Cookie对象visittimesCookie,用来记录登陆次数
Cookie visittimesCookie = new Cookie(“visitTimes”, “0″);
// 把usernameCookie对象信息添加到response中
response.addCookie(usernameCookie);
// 把对象visittimesCookie添加到response中,将cookie发送给客户端
response.addCookie(visittimesCookie);
// 显示Cookie页面
response.sendRedirect(request.getContextPath() + “/cookie.jsp”);
return;
}
%>
<body>
<div align=”center” style=”margin:10px; “>
<fieldset>
<legend>登录</legend>
<form action=”error.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>