5.15
package com.xxxx.session01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/s01")
public class Session01 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取session对象
HttpSession session = request.getSession();
// 获取session的会话标识符
String ID = session.getId();
System.out.println(ID);
// 获取session的创建时间
System.out.println(session.getCreationTime());
// 获取session 的最后访问时间
System.out.println(session.getLastAccessedTime());
// 判断是否是新的session对象
System.out.println(session.isNew());
}
}
package com.xxxx.session01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/s02")
public class Session02 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取session对象
HttpSession session = request.getSession();
// 设置session域对象
session.setAttribute("uname","admin");
session.setAttribute("upwd","123456");
// 移除session域对象
session.removeAttribute("upwd");
// request域对象
request.setAttribute("name","zhangsan");
// 跳转index.jsp中
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
package com.xxxx.session01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/s03")
public class Session03 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取session对象
HttpSession session = request.getSession();
// 设置session域对象
session.setAttribute("uname","admin");
//
System.out.println("session的最大不活动时间:"+ session.getMaxInactiveInterval());
//修改最大不活动时间
session.setMaxInactiveInterval(5);//5秒失效
// session.invalidate();//立即销毁
//关闭浏览器session失效
// 关闭服务器session失效
}
}