• Post Reply Bookmark Topic Watch Topic
  • New Topic

When does aHttpSession gets Created?  RSS feed

 
Vignesh Ramesh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a test servlet and all i do is add some attributes to servlet context and forward the request to JSP.

To Understand when a session object gets created i had a HttpSessionListner defined as follows

From the other posts i read that the HttpSession gets created by the container by default. but i dont see any of the sys out as defined at the HttpSessionListner.
I know that request.getSession(boolean) will return the session associated if any but unable to understand when does the session gets created initially.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read that the HttpSession gets created by the container by default

That is incorrect. Servlets do not create sessions by default; JSPs do. You can make that explicit via "<%@ page session="false" %>" (or true). You generally should turn that off, and create the session explicitly through Java code in your servlet.

Also, HttpSessionListeners need to be declared in web.xml; it's not clear from your post whether you did that. See https://www.coderanch.com/how-to/java/DeclaringListeners for some more information.
 
Vignesh Ramesh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have also defined the listner at web.xml

but i am still not clear if the session can only be created by calling request.getSession(boolean) or does the container creates the session. I am sure the listener code is correct and will be triggered if a session is created.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the session can only be created by calling request.getSession(boolean) or does the container creates the session.

The container creates a session IF request.getSession(true) is called OR one is created in the JSP under the circumstances I outlined in my previous post.
 
Vignesh Ramesh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in the example i provided, i have the servlet redirecting the request to jsp and i have not explcitly marked page session as false. so why is the session listner unable to print even when the jsp out is displayed when i hit the servlet.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave it a quick test, and the session is created in the JSP - meaning that the session listener reports 1 active session. Are you certain your mappings and everything are correct?
 
Vignesh Ramesh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Yes I have other listeners configured for session context attribues added/ removed and I am able to see the sysouts at tomcat'sconsole. Only unable to see the session related sys outs.

Reverfied and launched the same servlet from different browsers and was able to see two session created. Could you please help me understand when does the session actully created ? is it only when the request is forwarded to a JSP and when the servlet has request.getSession()y ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!