Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Kindly clarify "HttpSession.isNew()"

 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I submit a form from a jsp (say login.jsp) and get the parameters in a servlet(say loginServlet.java). Further i setup new attributes in the session and pass it back to another jsp(say mainmenu.jsp).

Now take that I get the session (say cricsession) from request object in the mainmenu.jsp.
Now what'll cricsession.isNew() method return.

JAVA API says that if the session is unaware to client then it returns true. According to me I have made changes to the session
in "loginServlet.java". So it should return true.

Is my understanding Correct???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:
According to me I have made changes to the session
in "loginServlet.java". So it should return true.


What do you mean when you say "I have made changed to the session"?
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added some attributes to the existing session.
Now does it mean that the session.isNew() method returns true??? since I've altered the session as it has come from client.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No that would not cause it to show as new.

The best way to learn about these methods is to read the API.
isNew

public boolean isNew()


Returns true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request.

Returns:
true if the server has created a session, but the client has not yet joined
Throws:
java.lang.IllegalStateException - if this method is called on an already invalidated session


Note the link in my signature
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
request.getSession(boolean) method returns,

case1) The existing session(if present) no matter whatever be the value of the boolean given.

case2) If there is no existing session and boolen is true it returns a new session.

case3) If there is no existing session and boolen is false it returns null.

***coming back to case 1) Take that there is an existing session and boolean is true, does it return a new session with session context and parameters set similar to that of the incoming existing session.


Note: Iam not very sure about the last part[***coming back to case 1)].

Kindly clarify.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:
***coming back to case 1) Take that there is an existing session and boolean is true, does it return a new session with session context and parameters set similar to that of the incoming existing session.


No. Nevertheless, it would be evil.

[Edited]
BTW, how did you get this sort of thought?
[ May 17, 2005: Message edited by: Adeel Ansari ]
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aneel,
That's because I do this in my code.

first in "login.jsp" I have a form and on submission of the form I go to "loginServlet.java". There I get the session from the request object and add some attributes to the obtained session(say cricSession). then the control goes to "mainmenu.jsp" (i use a forward here so that the client is unaware of the changes made to session).

Now in "mainmenu.jsp", I get the session object[using "HttpSession cricSession = request.getSession(true)"] and check if it new using "cricSession.isNew()" and it says it's new.

refer this mail thread for this.

Clarification would be appreciated.




 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the server creates a session in response to the first request, the session is in a new state. So, HttpSession.isNew() will return true.

On the next request, which must include the session ID received from the server, the server will associate the request with the session. The client will then have joined the session, meaning that the session is no longer in the new state. So, HttpSession.isNew() will return false.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chung Wee,
Is sessionID created in Server or in Browser?? Like a server identifies whether a session is new, is there any way that the browser can understand about the session details( say it is new or it's an already running session)
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the server creates a session, it assigns it a new session ID which is sent to the client with the response. The browser stores the session ID in a cookie.

Whether the browser can make any sensible use of the information stored in the cookie I can't tell you. But as you can get a reference to HttpSession and as you know that the isNew() method returns true, then this suggests that only one session request was made. Note that isNew() would always returns true if cookies are disabled. (I am assuming that you are not using URL rewriting.)
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun you need some JSP & Servlet tutorial. Try to search google. Hope you will get many.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, below is the brief explaination.

HttpSession.isNew()returns whether the session is new. A session is considered new if it has been created by the server but the client has not yet acknowledged joining the session.

For example, if a server supports only cookie-based sessions and a client has disabled the cookies, calls to request.getSession() method of always return new sessions.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session is new before first response I guess.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adeel.
Was just able to get hold of the session concept.
Just read the Session Management in J2EE.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic