JSP学习笔记七之Cookie

首先提一下http协议的无状态性指的是服务器不会记住已经给它发过请求的客户端。每次收到请求都会认为是一个新的客户端发过来的。(即:服务器不会记住给他发过请求的客户端)

所以这个时候我们就需要使用Cookie来保存用户的状态。

 

Cookie指web服务器保存在客户端的一系列文本信息。比如:判定注册用户是否已经登陆网站、网购购物车的处理等。所以消耗的是客户端的存储空间。

Session是通过服务器来保持状态的,是服务器端为客户端所开辟的存储空间。所以消耗的是服务器端的存储空间。

1、保存用户的状态的两大机制:cookie和session。

   a 、cookie作用:
1.对特定对象的追踪 
2.保存用户网页浏览记录与习惯
3.简化登录
不足的是安全风险:容易泄露用户信息

   b、session的作用

在创建了Session的同时,服务器会为该Session生成唯一的Session id,而这个Session id在随后的请求中会被用来重新获得已经创建的Session;在Session被创建之后,就可以调用Session相关的方法往Session中增加内容了,而这些内容只会保存在服务器中,发到客户端的只有Session id;当客户端再次发送请求的时候,会将这个Session id带上,服务器接受到请求之后就会依据Session id找到相应的Session,从而再次使用之。正这样一个过程,用户的状态也就得以保持了。

 

2、Cookie的常用的方法

创建Cookie对象: Cookie newCookie = new Cookie(String key,Object value);
写入cookie对象: response.addCookie(newCookie);
读取Cookie对象: Cookie[] cookies = request.getCookies();
设置Cookie对象的有效期(秒): setMaxAge()
创建Cookie后进行赋值 setValue(String value)
获取Cookie的名称 getName()
获取Cookie的 getValue()
获取Cookie的有效期(秒): getMaxAge()

 

3、下面给出一个Cookie的JSP实例。


login.jsp中使用一个checkbox来进行记录是否记住登陆状态。然后在dologin.jsp进行创建cookie,并且设置cookie的值和向服务器添加cookie实例、设置cookie对象的存活时间等。

dologin.jsp中会有超链接的存在,用于连接users.jsp。在users.jsp界面会显示刚刚在登陆界面输入的用户名和密码。这里通过checkbox是否被勾选来判断是否需要创建Cookie。程序的逻辑是checkbox的被选上就创建Cookie

 首先是登陆界面login.jsp


    <%
    	//获取Cookie实例对象中的元素值
    	Cookie[] cookie=request.getCookies();
    	String username="";
    	String password="";
    	if(cookie!=null && cookie.length>0){
    		for(Cookie c:cookie){
    			if(c.getName().equals("username")){
    				username=c.getValue();
    			}
    			if(c.getName().equals("password")){
    				password=c.getValue();
    			}
    		}
    	}
    %>
  <body>
    <h1>用户登录</h1>
    <hr>
        <form name="loginForm" action="dologin.jsp" method="post">
       <table>
         <tr>
           <td>用户名:</td>
           <td><input type="text" name="username" value="<%=username %>"/></td>
         </tr>
         <tr>
           <td>密码:</td>
           <td><input type="password" name="password" value="<%=password %>" /></td>
         </tr>
         <tr>
           <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td>
         </tr>
         <tr>
           <td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td>
         </tr>
       </table>
    </form>
  </body>


<span style="font-family: Arial, Helvetica, sans-serif;">dologin.jsp代码如下:</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html" style="color: rgb(20, 25, 30);"><span style="font-family: Arial, Helvetica, sans-serif;"> <body></span>
 
 
    <h1>登录成功</h1>
    <hr>
    <br>
    <br>
    <br>
	<%
	//首先判断用户是否记住了登陆状态
	String[] isUseCookie = request.getParameterValues("isUseCookie");
	if(isUseCookie!=null && isUseCookie.length>0){
		//将用户名和密码保存到Cookie中
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		//定义Cookie对象
		Cookie usernameCookie=new Cookie("username",username);
		Cookie passwordCookie=new Cookie("password",password);
		//设置Cookie对象的有效时间
		usernameCookie.setMaxAge(864000);//10天
		passwordCookie.setMaxAge(864000);//10天
		
		//向服务器中添加Cookie
		response.addCookie(usernameCookie);
		response.addCookie(passwordCookie);	
	}
	else{
		//检查之前是否有cookie存在
		Cookie[] cookie=request.getCookies();
		if(cookie!=null && cookie.length>0){
			//遍历Cookie
			for(Cookie c:cookie){
				if(c.getName().equals("username")||   //如果出现username和password的Cookie
						c.getName().equals("password")){
					c.setMaxAge(0);//将该cookie的时间设为0
					response.addCookie(c);//重新将Cookie添加到服务器中
				}
			}
		}
	}
	%>
    <a href="users.jsp" target="_blank">查看用户信息</a>
    
  </body>


users.jsp

结果显示:




不选择checkbox时的结果:




1、JSP常用的有page、include、taglib指令这三种指令

page:位于页面顶端,一个页面可以包含多个page指令
include:将一个外部文件嵌入jsp中,同时解析这个页面中的jsp语句。
taglib:使用标签库,自定义新的标签,在jsp中启动定制行为。

 

a、include指令 
语法 <% include file="地址"%>。
案例:显示当前时间的页面。步骤如下:

(1)写一个只输出时间的方法的date.jsp。

(2)用于显示的页面,需要包含<% include file="date.jsp"%>这句。

 

实例代码:

date.jsp

<%
//创建一个日期的实例
    Date d=new Date();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
	String s=sdf.format(d);
	out.println(s);
%>

inculde_command.jsp

<body>
    <h1>include指令的测试</h1><br>
    <%@ include file="date.jsp"%>
  </body>

结果显示如下:


5、include动作(动作标签)
<jsp:include page="URL" flush="true/false" />
page :要包含的页面
flush :被包含的页面是否从缓冲区读取

 

代码实例:

include_action.jsp

 <body>
    <h1>include动作的测试</h1><br>
    <jsp:include page="date.jsp" 					flush="false"></jsp:include>
  </body>


6、include指令和动作的比较:

include指令

jsp:include动作

语法格式

<%@ include file=””%>

<jsp:include page=””>

发生作用的时间

页面转换期间

请求期间

包含的内容

文件的实际内容

页面的输出

转换成Servlet

主页面和包含页面转换成一个Servlet

主页面和包含转换为独立的Servlet

编译时间

较慢-资源必须被解析

较快

执行时间

稍快

较慢-每次资源必须被解析


猜你喜欢

转载自blog.csdn.net/LULEI1217/article/details/50954351