JavaWeb学习(三)——Cookie、session、

session和cookie对象

由于HTTP是无状态连接,每一次请求的数据都是相互独立且不会被保存的。为解决诸如购物车的问题。引入了cookie和session对象。

cookie对象(不是内置对象)

Cookie 是由服务器产生的,再通过response对象设置到响应头返回给客户端,最后交由本地保存。起到表示状态的作用(如区分是否是第一次访问本网站、是否已经登录了)。

对象cookie
所属类java.servlet.http.Cookie
构造方法public Cookie(String name,String value)
获取nameString getName()
获取valueString getValue()
设置最大有效期void setMaxAge(int expiry)

流程:
1、 服务端准备Cookie,然后 通过response.addCookie(Cookie cookie)
2、客户端获取Cookie, 通过request.getCookies();

Session

Session是会话技术,实现在一次会话(打开浏览器到关浏览器 的过程就是一次会话(没有超过最大时长的情况下))保存信息。

session机制

1、客户端第一次访问服务端时,服务端会生成一个Session对象,用于保存客户 信息,并且每个Session对象都会有一个唯一的sessionIId,用于唯一识别。然后服务端会产生一个Cookie,该Cookie的name=JSESSIONID ,value= 服务端sessionId对应的值。然后服务端会在 响应客户端的同时将该Cookie对象发送给客户端,至此 客户端就有了 一个Cookie(JSESSIONID);
这样,客户端的Cookie对象就可以和服务端的Session对象一 一对应了(JSESSIONID - sessionID)

2、客户端第二/n次请求服务端时:服务端会先用客户端cookie中的JSESSIONID 去服务端的session中匹配sessionid,如果匹配成功(cookie jsessionid和sesion sessionid),说明此用户 不是第一次访问,无需登录;
在这里插入图片描述

使用cookie实现用户名自动记住

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%!
		String uname  ;
	%>
	<%
		boolean flag = false ;
		Cookie[] cookies = request.getCookies() ;//获取本地所有的cookie
		for(Cookie cookie :cookies){
			if(cookie.getName().equals("uname")){//获取key为uname的cookie值
				uname = cookie.getValue() ;
				flag = true ;
			}
		}
		
		if(!flag){//if(flag ==true)
			out.print("cookie已失效!");
		}else{
			out.print("cookie:"+uname);
		}
	
	%>


	<form action="check.jsp" method="post">
		用户名:<input type="text" name="uname"  value="<%=(uname==null?"":uname)%>"><br/>
		
		
		密码:<input type="password" name="upwd"><br/>
		<input type="submit" value="登陆"><br/>
		
	</form>
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%
			request.setCharacterEncoding("utf-8") ;
			String name = request.getParameter("uname");
			String pwd = request.getParameter("upwd");
			
			//将用户名 加入到Cookie种
			Cookie cookie = new Cookie("uname",name);
			
			cookie.setMaxAge(10) ;//设置生存cookie在本地保存的时效 单位秒
			
			response.addCookie(cookie) ;
			
			response.sendRedirect("A.jsp") ;
		
		%>
</body>
</html>

A.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

实现逻辑
在这里插入图片描述

不同浏览器之间的cookie有没有共享,要看浏览器的设置。上述案例中cookie就是没有共享。
在这里插入图片描述

Session案例

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="check.jsp" method="post">
		用户名:<input type="text" name="uname"><br/>
		密码:<input type="password" name="upwd"><br/>
		<input type="submit" value="登陆"><br/>
	</form>
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%
			request.setCharacterEncoding("utf-8") ;
			String name = request.getParameter("uname");
			String pwd = request.getParameter("upwd");
			if(name.equals("zs") && pwd.equals("abc")){//假设 zs abc
				//只有登录成功,session中才会存在uname /upwd
				session.setAttribute("uname", name); //session为内置对象 可直接获取
				session.setAttribute("upwd", pwd)		;
				System.out.println("sessionId"+session.getId());
				
				request.getRequestDispatcher("welcome.jsp").forward(request, response) ;
				
			
			}else{
				//登录失败
				response.sendRedirect("login.jsp") ;
			}
		
		%>
</body>
</html>

invalidate.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%
		
			session.invalidate() ;//session失效
			response.sendRedirect("login.jsp") ;
		%>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	欢迎您:
		<%
			String name = (String)session.getAttribute("uname") ;
			//如果 用户没有登录,而是直接 通过地址栏 访问welcome.jsp,则必然获取到的name是null
			if(name!=null){
				out.print(name);
				
				
				System.out.println();
		%>
			<a href="invalidate.jsp">注销</a>
		<%
				
			}else{//如果没有登录,应该跳转登录页面
				response.sendRedirect("login.jsp");
			}
			
		
		%>
</body>
</html>

a.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%

			out.print(session.getAttribute("uname"));
			Cookie[] cookies = request.getCookies();
			for(Cookie cookie:cookies){
				if(cookie.getName().equals("JSESSIONID")){
					System.out.print("JSESSIONID"+cookie.getValue());
				}
			}
		
		
		%>
</body>
</html>

移除session域中的某个属性: session.removeAttribute(“属性key”)。
可以看到Cookie的JSESSIONID的值与Session的sessionid值一样。

cookie 与session对象的比较

cookie
保存的位置客户端
安全性较差
保存内容String
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值