| Author |
Kindly clarify "HttpSession.isNew()"
|
Arun Kumarr
Ranch Hand
Joined: May 16, 2005
Posts: 508
|
|
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???
|
If you are not laughing at yourself, then you just didn't get the joke.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
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"?
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Arun Kumarr
Ranch Hand
Joined: May 16, 2005
Posts: 508
|
|
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
Joined: Dec 11, 2004
Posts: 13410
|
|
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
Joined: May 16, 2005
Posts: 508
|
|
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
Joined: Aug 15, 2004
Posts: 2874
|
|
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
Joined: May 16, 2005
Posts: 508
|
|
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
Joined: Sep 29, 2002
Posts: 1683
|
|
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.
|
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
|
 |
Arun Kumarr
Ranch Hand
Joined: May 16, 2005
Posts: 508
|
|
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
Joined: Sep 29, 2002
Posts: 1683
|
|
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
Joined: Aug 15, 2004
Posts: 2874
|
|
|
Arun you need some JSP & Servlet tutorial. Try to search google. Hope you will get many.
|
 |
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
|
|
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
Joined: Feb 07, 2004
Posts: 471
|
|
|
Session is new before first response I guess.
|
Get power of your iPod with MediaChest | Minimal J2EE container is here | Light weight full J2EE stack | My blog | Co-author of "Windows programming in Turbo Pascal"
|
 |
Arun Kumarr
Ranch Hand
Joined: May 16, 2005
Posts: 508
|
|
Thanks Adeel. Was just able to get hold of the session concept. Just read the Session Management in J2EE.
|
 |
 |
|
|
subject: Kindly clarify "HttpSession.isNew()"
|
|
|