JavaWeb-Cookie和Session
2021-01-21 05:13
标签:jessionid pre request `` 取数据 服务 except oid setpath 创建Cookie: 注意!Cookie中不能存放中文 java代码 获取Session对象 Session对象的声明周期 销毁: 总结:session是保存在服务器上的,而会话关闭后服务器上的session域无人认领只能等待自动销毁,将JESSIONID借助Cookie持久化到客户端中,就可以再次使用session域 Session-java代码 JavaWeb-Cookie和Session 标签:jessionid pre request `` 取数据 服务 except oid setpath 原文地址:https://www.cnblogs.com/torain/p/12897978.htmlCookie和Session
1、为了解决无状态的的弊端,因此需要会话技术来使服务器区分客户端。
2、从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话。Cookie
Cookie的处理
Cookie cookie = new Cookie(String cookieName,String cookieValue);
在CookieTest中存入Cookie,再到GetCookieInfo中获取package com.torain.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieTest extends HttpServlet {
/**
* 在本Servlet中持久化Cookie
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取Cookie对象
Cookie cookie = new Cookie("goods","iPad");
// 设置持久化时间
cookie.setMaxAge(10*60);
// 设置作用域
cookie.setPath("/WEBTEST");
// 发送Cookie
response.addCookie(cookie);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.torain.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCookieInfo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取Cookie
Cookie[] cookies = request.getCookies();
// 遍历Cookie获取goods值
for(Cookie cookie : cookies) {
if (cookie.getName().equals("goods")) {
response.getWriter().write(cookie.getValue());
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Session
HttpSession session = request.getSession();
此方法会获得专属于当前会话的Session对象,如果服务器端没有该会话的Session 对象会创建一个新的Session返回,如果已经有了属于该会话的Session直接将已有的Session返回(实质就是根据JSESSIONID判断该客户端是否在服务器上已经存在 session了)
session存取数据
session也是一个域对象使用域对象通用方法session.setAttribute(String name,Object obj);
session.getAttribute(String name);
session.removeAttribute(String name);
创建:
package com.torain.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionServletA extends HttpServlet {
/**
* 获取一个Session对象,存入值
* 将Session的JESSIONID持久化到客户端
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取Session对象
HttpSession session = request.getSession();
// 向Session域中存值
session.setAttribute("name", "张三");
// 将Session的JSESSIONID持久化到客户端
String id = session.getId();
Cookie cookie = new Cookie("JSESSIONID",id);
// 持久化时间3分钟
cookie.setMaxAge(180);
// 发送Seesion
response.addCookie(cookie);;
// 打印
response.getWriter().write("JSESSIONID:" + id);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
文章标题:JavaWeb-Cookie和Session
文章链接:http://soscw.com/index.php/essay/44837.html