????????会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话。常用的会话跟踪技术是Cookie与Session。Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份。
cookie是在浏览器中保存的
如果想要使用cookie要保证我们的浏览器是开启cookie,所以说有一定的弊端,如果浏览器没有开启cookie,就不能再使用cookie了
cookie的大小是有限制的,通常是4096byte
cookie的保存是以键值对的形式存在的
//1.cookie的构造方法,目的是实例化出来cookie对象
Cookie(String name,String value)
//2.设置cookie的方法
setValue(String value) //修改cookie的值
getValue(String value) //获得cookie的值 ? ?
getName(String value) //获得cookie的键 ? ?
setMaxAge(int time) //设置cookie的有效时间
//3.要将cookie发送到浏览器
response.addCookie(Cookie cookie);
//4.获得所有cookie
request.getCookies();
实例
SetCookieServlet类
public class SetCookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 1.创建cookie对象
* 将键:java2311 值:sb ,存到cookie对象中
*/
Cookie cookie = new Cookie("msg", "sb");
/**
* 2.设置有效时间
* 正数:表示当前cookie的有效时间
* 负数:表示当前浏览器打开的时候存在,关闭的时候没了
* 0:销毁当前的cookie
*/
cookie.setMaxAge(60*60*24);//设置了有效期是个正数,
//3.把cookie发送到浏览器
resp.addCookie(cookie);
}
}
web.xml
<servlet>
<servlet-name>setCookieServlet</servlet-name>
<servlet-class>com.by.servlet.SetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>setCookieServlet</servlet-name>
<url-pattern>/setCookieServlet</url-pattern>
</servlet-mapping>
结果
GetCookieServlet类
public class GetCookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取浏览器中cookie,返回值是一个数组
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
System.out.println("==============");
System.out.println(cookie.getName());//获取键
System.out.println(cookie.getValue());//获取值
}
}
}
web.xml
<servlet>
<servlet-name>getCookieServlet</servlet-name>
<servlet-class>com.by.servlet.GetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getCookieServlet</servlet-name>
<url-pattern>/getCookieServlet</url-pattern>
</servlet-mapping>
结果
DestroyCookieServlet类
public class DestroyCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//退出登录
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("msg")){
cookie.setMaxAge(0);//销毁cookie
//重新发送给浏览器
response.addCookie(cookie);
}
}
}
}
web.xml
<servlet>
<servlet-name>destroyCookie</servlet-name>
<servlet-class>com.by.servlet.DestroyCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>destroyCookie</servlet-name>
<url-pattern>/destroyCookie</url-pattern>
</servlet-mapping>
结果
? ? ? ? 1)是什么
? ? ? ? ? ? 保存在浏览器的一块存储区域
? ? ? ? 2)特点
? ? ? ? ? ? ②键值对
? ? ? ? ? ? ③可以禁用cookie
? ? ? ? ? ? ④最多存4096byte
? ? ? ? ? ? ⑤可设置过期时间
cookie保存数据类型是单一的,只能保存字符串类型的数据
cookie的大小有限制
保存服务器中
当用户发送一个HTTP请求到服务器时,服务器会检查该请求是否包含session标识符(通常是存cookie),如果没有,则会创建一个新的session(存储区域),并将session标识符发送给客户端。浏览器再发送请求时会携带session标识符,此时服务器根据session标识符就可以找到对应的session(存储区域)
使用session的时候一般要开启cookie如果浏览器没有开启cookie功能,我们可以通过html的url传参完后session的使用
没有大小的限制
信息的保存也是以键值对的形式存在的
request.getSession(true); //返回HttpSession
setAttribute(key,value); //设置属性 key ,value
getAttribute(key); //获取key的值
removeAttribute(String key)?? ?//通过key值删除数据
invalidate(); //将session中的变量全部清空
setMaxInactiveInterval(int interval); //设置session失效时间,单位为秒
getMaxInactiveInterval();//获取session失效时间
getId(); //获取sessionID
实例
SetSessionServlet类
public class SetSessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得session
HttpSession httpSession = req.getSession();
//获得sessionid
String sessionId = httpSession.getId();
System.out.println("sessionId:" + sessionId);
User user = new User();
user.setUsername("张5丰");
user.setBirthday(new Date());
user.setSex("1");
//往session中添加属性
httpSession.setAttribute("user", user);
//设置session的失效时间
httpSession.setMaxInactiveInterval(60*5);
}
}
web.xml
<servlet>
<servlet-name>setSessionServlet</servlet-name>
<servlet-class>com.by.servlet.SetSessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>setSessionServlet</servlet-name>
<url-pattern>/setSessionServlet</url-pattern>
</servlet-mapping>
结果
GetSessionServlet类
public class GetSessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得session
HttpSession httpSession = req.getSession();
//获得session中的属性
User user = (User) httpSession.getAttribute("user");
System.out.println(user);
}
}
web.xml
<servlet>
<servlet-name>getSession</servlet-name>
<servlet-class>com.by.servlet.GetSessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getSession</servlet-name>
<url-pattern>/getSession</url-pattern>
</servlet-mapping>
结果
DestroySessionServlet类
public class DestroySessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得session
HttpSession httpSession = req.getSession();
//session失效
httpSession.invalidate();
}
}
web.xml
<servlet>
<servlet-name>destorySession</servlet-name>
<servlet-class>com.by.servlet.DestroySessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>destorySession</servlet-name>
<url-pattern>/destorySession</url-pattern>
</servlet-mapping>
结果
? ? ? ? 1)是什么
? ? ? ? ? ? 是一个域对象,保存在服务器
? ? ? ? 2)特点
? ? ? ? ? ? ①键值对
? ? ? ? ? ? ②可设置过期时间
? ? ? ? ? ? ③失效:过期或关闭浏览器