Matthias Brantner wrote:
> HI
>
> > > why can a SessionId be null, or why can a Session be invalid. I just
> > > created a Session (req.getSession(true)) and I wanted to get a Session
> > > Id (sessionId = req.getRequestedSessionId()). So, how can this Session
> > > be invalid. All Cookies are accepted.
> > > Thanx in advance.
> > >
> >
> > You are not really asking the same question here.
> >
> > "request.getRequestedSessionId()" asks the servlet container "what session ID
> > was requested in this request?" The first time in, there won't be one, because
> > the browser does not know it needs to be in a session until you tell it (by
> > sending the cookie, or doing the appropriate URL rewriting).
> >
> > To find out the session id of the session you just created, call
> > session.getId() instead.
> But I requested a Session before. And sometimes it works, sometimes not!
>
It will work on the second or subsequent request for a session, but not the first.
It will also fail if the session was valid at one time, but is not any longer.
The request.getRequestedSessionId(), as it's name implies, asks for the session id
that was *requested* on this request -- whether or not it is valid. If the session
times out in between requests, or you invalidate it, request.getRequestedSessionId()
will still return a non-null id, but request.createSession(false) will return null
because there is no valid session.
If you want to know what ID was assigned to a session, ask the session itself:
String id = session.getId();
Of course, this knowledge doesn't really do you a lot of good because you cannot do
much useful with the id once you have it.
> Matthias
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html