关于session的一点小疑问。
package listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class WebListener implements ServletContextListener,HttpSessionListener,ServletRequestListener{ @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("ServletContext 已销毁"); } @Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("ServletContext 已初始化"); } @Override public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub System.out.println("Session已创建,ID: "+ arg0.getSession().getId()); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { // TODO Auto-generated method stub System.out.println("Session已销毁"); } @Override public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub System.out.println("HttpServeltRequest 已销毁"); } @Override public void requestInitialized(ServletRequestEvent sre) { HttpServletRequest req = (HttpServletRequest)sre.getServletRequest(); // TODO Auto-generated method stub System.out.println("HttpServeltRequest 已创建 uri: " + req.getRequestURI()); } }
package listener; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloServlet */ @WebServlet("/hello") public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().println("hello world<br>" ); request.getServletContext().setAttribute("context", "contextatt"); request.getSession().setAttribute("session", "sessionatt"); request.setAttribute("request", "requestatt"); response.getWriter().println(request.getSession().getId()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>listener-interface</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>listener.WebListener</listener-class> </listener> </web-app>
问下是不是如果不在servlet里面进行session的创建,默认是不会建立session这个东西的?
为什么我即使重启了服务器,然后同一个网页浏览器不关闭直接刷新,也不会再次触发Session创建,并且在servlet页面还是会打印出和上次一样的session id? 也就是说服务器直接认可了存储在cookie里面的session id么? 这个是为什么?
源自:监听器
2-3 内置对象监听器
35
收起
正在回答 回答被采纳积分+1
2回答
java工程师2020版
- 参与学习 人
- 提交作业 9397 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星