我知道它会阻止会话对象的创建,但是你什么时候需要这样做呢?当 JSP 不需要访问隐式会话时,它是否被认为是最佳实践?注意:我之所以问,是因为它是在这个 Spring MVC 教程中,我假设 springsource 的人知道他们的东西 - http://blog.springsour......" /> 我知道它会阻止会话对象的创建,但是你什么时候需要这样做呢?当 JSP 不需要访问隐式会话时,它是否被认为是最佳实践?注意:我之所以问,是因为它是在这个 Spring MVC 教程中,我假设 springsource 的人知道他们的东西 - http://blog.springsour......"> 我知道它会阻止会话对象的创建,但是你什么时候需要这样做呢?当 JSP 不需要访问隐式会话时,它是否被认为是最佳实践?注意:我之所以问,是因为它是在这个 Spring MVC 教程中,我假设 springsource 的人知道他们的东西 - http://blog.springsour......" />
ChatGPT解决这个技术问题 Extra ChatGPT

Why set a JSP page session = "false" directive?

I was wondering when you would want to set the following page directive in a JSP:

<%@ page session="false" %>

I know that it prevents the creation of the session object, but when would you need to do that? Is it considered a best practice when a JSP does not need to access the implicit session?

NOTE: The reason why I ask, is because it was in this Spring MVC tutorial and I assume the springsource folks know their stuff - http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/


n
no.good.at.coding

One reason would be performance and memory. If you have a page that doesn't need to be involved in a session (like say, an about.jsp or faq.jsp) then the default behaviour of involving every JSP in a session will impose the overhead of creating a new session object (if one doesn't already exist) and increased memory usage as more objects reside on the heap.

This effect will be greatly exaggerated in case of a single page seeing high traffic from many unique users combined with a high bounce rate i.e. they users do not continue to browse but leave the site immediately after viewing that one page- the container will create a new session object per user which will never be used again and will ultimately be garbage collected after it times out - added over head of object creation, memory usage and garbage collection without giving you any real value.


I really enjoy learning the small performance tips, they can add up to big things. Thanks!
Additionally, the session may be written to a persistent store for app servers in a fault tolerant setup, so specifying that a page does not use a session (not specifically that is does not create a session as the questioner implies) avoids the I/O overhead of interacting with the persistent store.
Amazing Explanation !!
i
informatik01

This setting is also a security measure, as it also avoids a potential DoS attack. Think about a simple script that iteratively wgets the JSP: it will generate a lot of sessions in few seconds.


R
Reimius

I actually have a real scenario in my app for its usage. We have Squid acting as a reverse proxy in front of our application. The squid server is set up to poll all the tomcat instances hosting our application to verify that the servers are up and running, if they are not, Squid will fail over to using another server in our cluster.

The actual polling to our app from Squid is set to poll a specific page in the app. Since Squid's poll is not actually a browser, it can't hold a session, which means that each poll to the server page would have tomcat create a session which Squid cannot hold a reference to. We add the <%@ page session="false" %> directive so that a session is not created on each poll. If we did not use this directive, we would have thousands of sessions created over 4 hours time for no reason.


H
Hein Blöd

Yet another use case where it is actually required to add this directive is when using Apache Shiro's noSessionCreation filter in the .ini configuration file, e.g. because your authentication scheme is stateless. If you lack it, you'll run into a org.apache.shiro.subject.support.DisabledSessionException.


a
austinbruch

Ran into another use case in my production application, figured I'd share it here in case it helps somebody.

We have a Web UI app that protects most resources via session. However, some resources are protected by part of the web tier that sits in front of our app in our production deployment. Therefore, as far as the app is concerned, these resources are totally unprotected. Some of these "unprotected" resources are JSPs.

In the case where a user establishes a session on one of our protected resources, then makes an XHR call from the browser to one of the "unprotected" resources, we were hitting an issue where the container claims that an anonymous user is trying to access a session of user foo, thus stopping execution. Configuring the "unprotected" JSP to not use sessions got us around this problem.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now