我今天才注意到,每次 Tomcat(8) 启动时,它都会创建一个新的 HttpSession
(没有任何 HttpServletRequest
)。
我只是像这样添加了一个 SessionListener:
public class SessionListener implements HttpSessionListener {
public SessionListener() {}
public void sessionCreated(HttpSessionEvent sessionEvent) {
HttpSession session = sessionEvent.getSession();
ServletContext context = session.getServletContext();
try {
if(session.isNew()){
System.out.println("a new Session is created");
}
} catch (Exception e) {}
}
public void sessionDestroyed(HttpSessionEvent sessionEvent) {}
}
我今天唯一改变的是 context.xml
中的这个:
<Context>
<Resource name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxActive="-1"
minIdle="-1"
maxWait="10000"
initialSize="10"
username="XYZ"
password="XYZ"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</Context>
但最后一个不可能是在容器启动时创建 session 的原因,对吧?
最佳答案
当您从像 Eclipse 这样的 IDE 中运行服务器时,就会发生这种情况。 IDE 的服务器插件(例如 Eclipse Tomcat 插件)可能会在启动过程后通过向 /
发送 GET 请求来执行自检(因此 IDE 服务器插件可以将服务器标记为“已启动”)。显然,您在 /
页面上(隐式地)创建了一个新 session 。
忽略它。这不会在生产过程中发生。
关于jsf - Tomcat 8 在启动时创建一个新 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517831/