会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会话;
例如网站的登陆即使用了该功能,来方便用户的登陆操作。
cookie:一般会保存在本地的 用户目录下 appdata;
一个网站cookie是否存在上限!聊聊细节问题
删除Cookie;
注意
@WebServlet("/cook1")
public class CookieTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置字符编码
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
PrintWriter out= resp.getWriter();
//Cookie 服务器从客户端获取
Cookie[] cookies = req.getCookies();
//判断Cookie是否存在
if(cookies!=null){
out.write("你上次访问的时间是:");
for (int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
//获取cookie的名字
if ("lastLoginTime".equals(cookie.getName())){
long l = Long.parseLong(cookie.getValue());
Date date = new Date(l);
out.write(date.toLocaleString());
}
}
}else {
out.write("创建cookie");
}
//服务器给客户端响应Cookie
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
}
@WebServlet("/sess01")
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//获取session
HttpSession session = req.getSession();
//设置值
session.setAttribute("name", "张三");
//获取session的id
String id = session.getId();
//判断session是否新创建
if (session.isNew()) {
resp.getWriter().write("session创建成功!id=" + id);
} else {
resp.getWriter().write("session已经存在了,id=" + id);
}
resp.getWriter().write(session.getAttribute("name").toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在Session中保存一个对象和一个字符串
@WebServlet("/sess01")
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//获取session
HttpSession session = req.getSession();
//设置值
session.setAttribute("name", "张三");
session.setAttribute("person",new Person("李四",20));
//获取session的id
String id = session.getId();
//判断session是否新创建
if (session.isNew()) {
resp.getWriter().write("session创建成功!id=" + id);
} else {
resp.getWriter().write("session已经存在了,id=" + id);
}
resp.getWriter().write(session.getAttribute("name").toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
@WebServlet("/sess02")
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到Session
HttpSession session = req.getSession();
String name = (String) session.getAttribute("name");
Person person = (Person) session.getAttribute("person");
System.out.println(person.toString());
System.out.println(name);
}
}
手动清除session中的参数
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
String name = (String) session.getAttribute("name");
System.out.println(name);
session.removeAttribute("name");
session.invalidate();
}
}
设置自动Session自动保存的最大时长(web.xml)
<session-config>
<!--设置Session10分钟后自动失效-->
<session-timeout>10</session-timeout>
</session-config>
原文:https://www.cnblogs.com/saxonsong/p/14811821.html