|
| 1 | +package com.atguigu.servlet; |
| 2 | + |
| 3 | +import javax.servlet.ServletException; |
| 4 | +import javax.servlet.http.HttpServletRequest; |
| 5 | +import javax.servlet.http.HttpServletResponse; |
| 6 | +import javax.servlet.http.HttpSession; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +public class SessionServlet extends BaseServlet{ |
| 10 | + |
| 11 | + /** 往session域中保存保存数据 |
| 12 | + * |
| 13 | + * @param req |
| 14 | + * @param resp |
| 15 | + * @throws ServletException |
| 16 | + * @throws IOException |
| 17 | + */ |
| 18 | + protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 19 | + //getSession()第一次是创建Session,之后调用都是获取之前创建的Session |
| 20 | + req.getSession().setAttribute("key1","value1"); |
| 21 | + resp.getWriter().write("已经往Session域中保存了数据"); |
| 22 | + } |
| 23 | + |
| 24 | + /** 从Session域中获取数据 |
| 25 | + * |
| 26 | + * @param req |
| 27 | + * @param resp |
| 28 | + * @throws ServletException |
| 29 | + * @throws IOException |
| 30 | + */ |
| 31 | + protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 32 | + Object attribute= req.getSession().getAttribute("key1"); |
| 33 | + resp.getWriter().write("从Session域中获取到的key1的数据是:"+attribute); |
| 34 | + } |
| 35 | + |
| 36 | + /** 创建或获取 Session |
| 37 | + * |
| 38 | + * @param req |
| 39 | + * @param resp |
| 40 | + * @throws ServletException |
| 41 | + * @throws IOException |
| 42 | + */ |
| 43 | + protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 44 | + //创建和获取Session会话对象 |
| 45 | + HttpSession session=req.getSession(); |
| 46 | + //判断当前session是否是新创建的 |
| 47 | + boolean isNew=session.isNew(); |
| 48 | + //获取Session唯一标识id |
| 49 | + String id= session.getId(); |
| 50 | + resp.getWriter().write("得到的Session,它的id是:"+id +"<br/>"); |
| 51 | + resp.getWriter().write("这个Session是否是新创建的:"+isNew +"<br/>"); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 58 | + int maxInactiveInterval = req.getSession().getMaxInactiveInterval();//默认生命周期 |
| 59 | + //这里超时时长是1800秒即30分钟 ,因为tomcat的web.xml文件里 有默认配置: |
| 60 | + /* |
| 61 | + <session-config> |
| 62 | + <session-timeout>30</session-timeout> |
| 63 | + </session-config> |
| 64 | + */ |
| 65 | + //如果要改的话,直接在idea的xml文件里改就行 ,单独作用于当前xml的话就利用上面的api,getMaxInactiveInterval(int ) |
| 66 | + |
| 67 | + resp.getWriter().write("Session的默认超时时长为"+maxInactiveInterval+ "秒"); |
| 68 | + |
| 69 | + } |
| 70 | + protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 71 | + HttpSession session = req.getSession(); |
| 72 | + req.getSession().setMaxInactiveInterval(3); |
| 73 | + resp.getWriter().write("已经设置超时时长为3秒"); |
| 74 | + } |
| 75 | + |
| 76 | + protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 77 | + HttpSession session = req.getSession(); |
| 78 | + req.getSession().invalidate(); |
| 79 | + resp.getWriter().write("已经设置session为无效"); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments