session和cookie对象
由于HTTP是无状态连接,每一次请求的数据都是相互独立且不会被保存的。为解决诸如购物车的问题。引入了cookie和session对象。
cookie对象(不是内置对象)
Cookie 是由服务器产生的,再通过response对象设置到响应头返回给客户端,最后交由本地保存。起到表示状态的作用(如区分是否是第一次访问本网站、是否已经登录了)。
对象 | cookie |
---|---|
所属类 | java.servlet.http.Cookie |
构造方法 | public Cookie(String name,String value) |
获取name | String getName() |
获取value | String 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 |