JSP学习笔记七之Cookie

首先提一下http协议的无状态性指的是server不会记住已经给它发过请求的client。

每次收到请求都会觉得是一个新的client发过来的。

(即:server不会记住给他发过请求的client)

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

 

Cookie指webserver保存在client的一系列文本信息。比方:判定注冊用户是否已经登陆站点、网购购物车的处理等。所以消耗的是client的存储空间。

Session是通过server来保持状态的,是server端为client所开辟的存储空间。

所以消耗的是server端的存储空间。

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

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

   b、session的作用

在创建了Session的同一时候,server会为该Session生成唯一的Session id,而这个Session id在随后的请求中会被用来又一次获得已经创建的Session;在Session被创建之后。就能够调用Session相关的方法往Session中添加内容了,而这些内容仅仅会保存在server中,发到client的仅仅有Session id;当client再次发送请求的时候。会将这个Session id带上,server接受到请求之后就会根据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的值和向server加入cookie实例、设置cookie对象的存活时间等。

dologin.jsp中会有超链接的存在。用于连接users.jsp

users.jsp界面会显示刚刚在登陆界面输入的username和password。

这里通过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){
		//将username与password保存到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天
		
		//向server中加入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加入到server中
				}
			}
		}
	}
	%>
    <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

编译时间

较慢-资源必须被解析

较快

运行时间

稍快

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


原文地址:https://www.cnblogs.com/blfbuaa/p/7162145.html