文章目录
- 1.Session基本介绍
- 1.Session有什么用?
- 2.Session基本原理
- 3.Session可以做什么?
- 4.如何理解Session?
- 5. Session基本使用
- 2.Session底层机制
- 1.示意图
- 2.创建session底层机制
- 1.有sessionid,但没session对象
- 1.由于访问了jsp的主页面,会导致jsp创建一个sessionid,重新发布之后会将session对象删除
- 2.代码实例
- 2.有sessionid,有session对象
- 1.刚才已经创建了一个新的session对象并绑定了一个新的sessionid,返回给浏览器,所以现在的浏览器既有sessionid又有session对象
- 2.代码实例(还是刚才的代码)
- 3.没有sessionid,没有session对象
- 1.更换一个浏览器,此时这个浏览器并没有携带这个服务器的sessionid
- 2.代码实例(还是刚才的代码)
- 3.读取session底层机制
- 3.Session的生命周期
- 1.基本说明
- 一个浏览器同一时间只能有一个session!!!
- 2.设置指定过期时间
- 1.CreateSession2.java
- 2.readSession2.java
- 3.结果演示
- 3.删除session
- 4.Session作业
- 1.login.html
- 2.error.html
- 3.LoginCheckServlet.java
- 4.ManageServlet.java
- 5.结果展示
- 5.Session小结
- 关于getSession的理解
1.Session基本介绍
1.Session有什么用?
2.Session基本原理
3.Session可以做什么?
4.如何理解Session?
5. Session基本使用
2.Session底层机制
1.示意图
2.创建session底层机制
1.有sessionid,但没session对象
1.由于访问了jsp的主页面,会导致jsp创建一个sessionid,重新发布之后会将session对象删除
2.代码实例
package session;import sun.security.jgss.HttpCaller;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.DataOutput;
import java.io.IOException;
import java.io.PrintWriter;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/createSession")
public class CreateSession extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取session,并放值HttpSession session = req.getSession();session.setAttribute("session", "hello");resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>session设置成功</h1>");//关闭writer.flush();writer.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
2.有sessionid,有session对象
1.刚才已经创建了一个新的session对象并绑定了一个新的sessionid,返回给浏览器,所以现在的浏览器既有sessionid又有session对象
2.代码实例(还是刚才的代码)
3.没有sessionid,没有session对象
1.更换一个浏览器,此时这个浏览器并没有携带这个服务器的sessionid
2.代码实例(还是刚才的代码)
3.读取session底层机制
package session;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;
import java.io.PrintWriter;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/readSession")
public class readSession extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//读取sessionHttpSession session = req.getSession();Object session1 = session.getAttribute("session");//先判断一下是不是为nullif (session1 != null) {String res = (String) session1;resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>"+ res +"</h1>");//关闭writer.flush();writer.close();}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
3.Session的生命周期
1.基本说明
一个浏览器同一时间只能有一个session!!!
2.设置指定过期时间
1.CreateSession2.java
package session;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;
import java.io.PrintWriter;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/createSession2")
public class CreateSession2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//创建sessionHttpSession session = req.getSession();session.setAttribute("name", "lihua");//设置生命周期session.setMaxInactiveInterval(30);resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>session设置成功</h1>");//关闭writer.flush();writer.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
2.readSession2.java
package session;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;
import java.io.PrintWriter;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/readSession2")
public class readSession2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//读取sessionHttpSession session = req.getSession();Object name = session.getAttribute("name");//一定要判断不是空再返回if (name != null) {String res = (String) name;resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>"+ res +"</h1>");//关闭writer.flush();writer.close();}else {resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>该session不存在</h1>");//关闭writer.flush();writer.close();}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
3.结果演示
60秒后
3.删除session
package session;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;
import java.io.PrintWriter;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/deleteSession")
public class deleteSession extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取session,并设置立即过期(删除session)HttpSession session = req.getSession();session.invalidate();resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>session删除成功</h1>");//关闭writer.flush();writer.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
4.Session作业
1.login.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>用户登录界面</h1>
<form action="check" method="get">用户名:<input type="text" name="username"><br>密码:<input type="password" name="password"><br><input type="submit" value="登录">
</form>
</body>
</html>
2.error.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>登录失败</h1><br>
<a href="login.html">点击返回重新登录!</a>
</body>
</html>
3.LoginCheckServlet.java
package session;import javax.servlet.RequestDispatcher;
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;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/check")
public class LoginCheckServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取用户名和密码String username = req.getParameter("username");String password = req.getParameter("password");//进行验证if (password.equals("666666")) {//验证成功则设置sessionHttpSession session = req.getSession();session.setAttribute("username",username);//重定向到欢迎页面resp.sendRedirect("ManageServlet");}else {resp.sendRedirect("error.html");}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
4.ManageServlet.java
package session;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;
import java.io.PrintWriter;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = "/ManageServlet")
public class ManageServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//根据session中的信息判断是否是管理员HttpSession session = req.getSession();Object state = session.getAttribute("username");//如果是空的,则说明不是管理员if (state == null) {resp.sendRedirect("error.html");}String name = (String) state;resp.setContentType("text/html;charset=utf-8;");PrintWriter writer = resp.getWriter();writer.print("<h1>用户管理页面</h1><br>");writer.print("<h3>欢迎你 管理员" + name +"</h3><br>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
5.结果展示
5.Session小结
关于getSession的理解
- 一个浏览器同时只能有一个session
- 注意,此时只要session不过期,那么这个浏览器每次请求服务的时候都会带上这个sessionid
- 首先会判断浏览器是否有sessionid
- 没有sessionid则会在浏览器端创建session对象并且将sessionid响应给浏览器(立即生效)
- 有sessionid则会判断服务器端是否有session对象
- 如果有session对象则直接使用
- 如果没有session对象则创建新的session对象并将新的sessionid响应给浏览器(立即生效)
- 这里立即生效的解释是,只要设置了session,就算进行了请求转发了,并没有进行第二次请求,那么转发到的那个servlet是可以立即读取到这个session的