什么是Session:
服务器会给每一个用户(浏览器)创建一个Seesion对象;
一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
用户登录之后,整个网站它都可以访问! -->保存用户的信息;保存购物车的信息....
Session和cookie的区别:
Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
Session把用户的数据写到用户独占Session中,服务器端保存 (保存重要的信息,减少服务器资源的浪费)
Session对象由服务创建;
使用场景:
保存一个登录用户的信息;
购物车信息;
在整个网站中经常会使用的数据,我们将它保存在Session中;
代码:
SessionDemo01:注册session
package com.allen.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; 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中存东西 session.setAttribute("name", "李庆林"); //获取session的id String sessionId = session.getId(); //判断session是不是新创建的 if (session.isNew()){ resp.getWriter().write("session创建成功:id = " + sessionId); }else{ resp.getWriter().write("session已经在服务器中存在了:id = " + sessionId); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
SessionDemo02:获取session中的信息
package com.allen.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; 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"); resp.getWriter().write(name); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
SessionDemo03:注销session
package com.allen.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo03 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.removeAttribute("name"); //手动注销session session.invalidate(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
web.xml
<servlet> <servlet-name>SessionDemo01</servlet-name> <servlet-class>com.allen.servlet.SessionDemo01</servlet-class> </servlet> <servlet-mapping> <servlet-name>SessionDemo01</servlet-name> <url-pattern>/s1</url-pattern> </servlet-mapping> <servlet> <servlet-name>SessionDemo02</servlet-name> <servlet-class>com.allen.servlet.SessionDemo02</servlet-class> </servlet> <servlet-mapping> <servlet-name>SessionDemo02</servlet-name> <url-pattern>/s2</url-pattern> </servlet-mapping> <servlet> <servlet-name>SessionDemo03</servlet-name> <servlet-class>com.allen.servlet.SessionDemo03</servlet-class> </servlet> <servlet-mapping> <servlet-name>SessionDemo03</servlet-name> <url-pattern>/s3</url-pattern> </servlet-mapping>
设置session自动过期时间:
<!-- 设置session过期时间 --> <session-config> <!-- 15分钟后session自动过期,以分钟为单位 --> <session-timeout>15</session-timeout> </session-config>
测试:
首先注册session:
获取session中的值:
注销session:
注销之后再访问s2获取session中的值:
此时的session已经不存在了。