日期:2014-05-16  浏览次数:20572 次

CH5——jsp中的cookie应用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="login.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	request.setCharacterEncoding("UTF-8");
	String username="";
	int visitTimes = 0;
	Cookie[] cookies = request.getCookies();
	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());
		}
	}
	if(username==null || username.trim().equals("")){
		throw new Exception("您还没有登陆,请先登录");
}
	Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(++visitTimes));
	response.addCookie(visitTimesCookie);
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>登录页面</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>

?

<%@ 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;
	}
 %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!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.png" /></span>
   								<span style="color: red;"&gt<%=exception.getMessage() %&gt</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>
   		</fiel