配置
如果说。你希望你的web工程,默认的session的超时时长为其他时长。你可以在你自己的web.xml配置文件中做 以上相同的配置。就可以修改你的web工程所有Seession的默认超时时长。
<!--表示当前web工程。创建出来的所有Session默认是20分钟超时时长--> <session-config> <session-timeout>20</session-timeout> </session-config>
结果
session3秒超时销毁
如果你想只修改个别session的超时时长。就可以使用上面的API setMaxInactiveInterval来进行单独的设置。
session.setMaxlnactivelnterval(int interval)单独设置超时时长。
超时的说明
修改 session.html
<!DocTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//en" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache"/> <meta http-equiv="Expires" content="0" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Session</title> <base href="http://localhost:8080/13_cookie_session/"> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="sessionServlet?action=createOrGetSession" target="target">Session的创建和获取(id号、是否为新创建)</a></li> <li><a href="sessionServlet?action=setAttribute" target="target">session域数据的存储</a></li> <li><a href="sessionServlet?action=getAttribute" target="target">Session域数据的获取</a></li> <li>session的存活</li> <li> <ul> <li><a href="sessionServlet?action=defaultLife" target="target">Session的默认超时及配置</a></li> <li><a href="sessionServlet?action=life3" target="target">session3秒超时销毁</a></li> <li><a href="" target="target">Session马上销毁</a></li> </ul> </li> <li><a href="" target="target">浏览器和Session绑定的原理</a></li> </ul> </div> </body> </html>
修改 SessionServlet
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionServlet extends BaseServlet{ protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建和获取Session会话对象 HttpSession session = req.getSession(); //判断 当前session回滚,是否是新创建出来的 boolean isNew = session.isNew(); //获取session会话的唯一标识 id String id = session.getId(); resp.getWriter().write("得到的Session,它的id是:"+id+"<br/>"); resp.getWriter().write("这个Session是否是新创建的:"+isNew+"<br/>"); } /** * 在Session中保存数据 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getSession().setAttribute("key1","value1"); resp.getWriter().write("已经往Session中保存了数据"); } /** * 获取Session域中的数据 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Object attribute = req.getSession().getAttribute("key1"); resp.getWriter().write("从Session中获取key1的数据是"+attribute); } protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取了Session的默认超时时长 int maxInactiveInterval = req.getSession().getMaxInactiveInterval(); resp.getWriter().write("Session的默认超时时长是:"+maxInactiveInterval+"秒"); } protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先获取Session对象 HttpSession session = req.getSession(); //设置当前Session3秒后超时 session.setMaxInactiveInterval(3); resp.getWriter().write("当前Session已经设置为3秒后超时"); } }
结果
创建新的true->获取是否为新创建false–>3秒销毁—>获取是否为新创建true
Session马上销毁
修改 SessionServlet
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionServlet extends BaseServlet{ protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建和获取Session会话对象 HttpSession session = req.getSession(); //判断 当前session回滚,是否是新创建出来的 boolean isNew = session.isNew(); //获取session会话的唯一标识 id String id = session.getId(); resp.getWriter().write("得到的Session,它的id是:"+id+"<br/>"); resp.getWriter().write("这个Session是否是新创建的:"+isNew+"<br/>"); } /** * 在Session中保存数据 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getSession().setAttribute("key1","value1"); resp.getWriter().write("已经往Session中保存了数据"); } /** * 获取Session域中的数据 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Object attribute = req.getSession().getAttribute("key1"); resp.getWriter().write("从Session中获取key1的数据是"+attribute); } protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取了Session的默认超时时长 int maxInactiveInterval = req.getSession().getMaxInactiveInterval(); resp.getWriter().write("Session的默认超时时长是:"+maxInactiveInterval+"秒"); } protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先获取Session对象 HttpSession session = req.getSession(); //设置当前Session3秒后超时 session.setMaxInactiveInterval(3); resp.getWriter().write("当前Session已经设置为3秒后超时"); } protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先获取Session对象 HttpSession session = req.getSession(); //让Session对象马上超时 session.invalidate(); resp.getWriter().write("Session已经设置为超时(无效)"); } }
修改 session.html
<!DocTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//en" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache"/> <meta http-equiv="Expires" content="0" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Session</title> <base href="http://localhost:8080/13_cookie_session/"> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="sessionServlet?action=createOrGetSession" target="target">Session的创建和获取(id号、是否为新创建)</a></li> <li><a href="sessionServlet?action=setAttribute" target="target">session域数据的存储</a></li> <li><a href="sessionServlet?action=getAttribute" target="target">Session域数据的获取</a></li> <li>session的存活</li> <li> <ul> <li><a href="sessionServlet?action=defaultLife" target="target">Session的默认超时及配置</a></li> <li><a href="sessionServlet?action=life3" target="target">session3秒超时销毁</a></li> <li><a href="sessionServlet?action=deleteNow" target="target">Session马上销毁</a></li> </ul> </li> <li><a href="" target="target">浏览器和Session绑定的原理</a></li> </ul> </div> </body> </html>
结果
创建新的true->获取是否为新创建false–>马上销毁—>获取是否为新创建true
m)浏览器和Session之间关联的技术内幕
session技术,底层其实是基于Cookie技术来实现的。