You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Carlos Alonso Vega <lo...@unavarra.es> on 2006/06/02 10:38:38 UTC

Retrieve certain session data inside a servlet

I am trying to obtain data about a session inside a servlet, the code 
seems to be simple as

localSession = Ctx.getManager().findSession(sessionId);

My problem is that I do not know how to obtain the Ctx object from a 
request (HttpServletRequest)

Thanks in advance



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by Carlos Alonso Vega <lo...@unavarra.es>.
Hi all,

 Thanks for all replies they help me a lot.

Regards
Carlos


Martin Gainty escribió:

> Good Morning Eric and Carlos-
>
> Indeed there is a way to enable CrossContext communication via the 
> CrossContext="true" directive inside the Context tag
> <Context path="/pluto" crossContext="true" />
>
> This capability is available from combined engineering staff from Sun 
> Microsystems and the IBM Corporation.
> Tthe specific JSR-168 spec is published at
> http://jcp.org/aboutJava/communityprocess/final/jsr168/
>
> For specific implementation details I would encourage you to look at 
> John Lewis' article available at
> http://www.theserverside.com/tt/articles/article.tss?l=ClusteringTomcat
>
> HTH.
> Martin--
>
> This email message and any files transmitted with it contain confidential
> information intended only for the person(s) to whom this email message is
> addressed.  If you have received this email message in error, please 
> notify
> the sender immediately by telephone or email and destroy the original
> message without making a copy.  Thank you.
>
> ----- Original Message ----- From: "Eric Haszlakiewicz" 
> <er...@swapsimple.com>
> To: "Carlos Alonso Vega" <lo...@unavarra.es>
> Cc: "Tomcat Users List" <us...@tomcat.apache.org>
> Sent: Friday, June 02, 2006 9:11 PM
> Subject: Re: Retrieve certain session data inside a servlet
>
>
>> On Fri, Jun 02, 2006 at 11:15:03AM +0200, Carlos Alonso Vega wrote:
>>
>>> Bob,
>>>
>>> Thanks for the reply. My problem is that the session info I need is not
>>> of the session that makes the request. I need to check other 
>>> sessions in
>>> the same context.  I can use the request because it is in the same
>>> context, so the manager is the same.
>>>
>>> This servlet is called internally from the app, and checks if some
>>> session exists. It is similar to the case of obtaining all active
>>> sessions from one context using the manager inside the servlet code
>>> (findSessions). If someone have this piece of code (or similar), it 
>>> will
>>> serve me.
>>
>>
>> To do something like that you need to do it yourself.  i.e. store a
>> HashTable in the application context, then add and remove sessions to
>> it when people login and logout, or at some other convinient time.
>> Of course, this assumes your app isn't running in a distributed 
>> environment.
>> If it is, then it's impossible to get a session object from a different
>> JVM.  (well, you could serialize it and transfer the data, but it won't
>> be the same object and it won't be useful for much other than extracting
>> some info)
>>
>> eric
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by Martin Gainty <mg...@hotmail.com>.
Good Morning Eric and Carlos-

Indeed there is a way to enable CrossContext communication via the 
CrossContext="true" directive inside the Context tag
<Context path="/pluto" crossContext="true" />

This capability is available from combined engineering staff from Sun 
Microsystems and the IBM Corporation.
Tthe specific JSR-168 spec is published at
http://jcp.org/aboutJava/communityprocess/final/jsr168/

For specific implementation details I would encourage you to look at John 
Lewis' article available at
http://www.theserverside.com/tt/articles/article.tss?l=ClusteringTomcat

HTH.
Martin--

This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.

----- Original Message ----- 
From: "Eric Haszlakiewicz" <er...@swapsimple.com>
To: "Carlos Alonso Vega" <lo...@unavarra.es>
Cc: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Friday, June 02, 2006 9:11 PM
Subject: Re: Retrieve certain session data inside a servlet


> On Fri, Jun 02, 2006 at 11:15:03AM +0200, Carlos Alonso Vega wrote:
>> Bob,
>>
>> Thanks for the reply. My problem is that the session info I need is not
>> of the session that makes the request. I need to check other sessions in
>> the same context.  I can use the request because it is in the same
>> context, so the manager is the same.
>>
>> This servlet is called internally from the app, and checks if some
>> session exists. It is similar to the case of obtaining all active
>> sessions from one context using the manager inside the servlet code
>> (findSessions). If someone have this piece of code (or similar), it will
>> serve me.
>
> To do something like that you need to do it yourself.  i.e. store a
> HashTable in the application context, then add and remove sessions to
> it when people login and logout, or at some other convinient time.
> Of course, this assumes your app isn't running in a distributed 
> environment.
> If it is, then it's impossible to get a session object from a different
> JVM.  (well, you could serialize it and transfer the data, but it won't
> be the same object and it won't be useful for much other than extracting
> some info)
>
> eric
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
I may be wrong about this, but I thought that it was expressly forbidden 
by the servlet spec to be able to get arbitrary sessions.  That is, even 
if you store the session ID in some table, you wouldn't be able to get 
any session other than that associated with the current request.

Now, that refers to standards-compliant ways to do it... I'm sure most 
app servers give you their own way to pull it off... JMX pops to mind as 
a likely possibility.

(unless I'm wrong-LOL)

Frank

Eric Haszlakiewicz wrote:
> On Fri, Jun 02, 2006 at 11:15:03AM +0200, Carlos Alonso Vega wrote:
>> Bob,
>>
>> Thanks for the reply. My problem is that the session info I need is not 
>> of the session that makes the request. I need to check other sessions in 
>> the same context.  I can use the request because it is in the same 
>> context, so the manager is the same.
>>
>> This servlet is called internally from the app, and checks if some 
>> session exists. It is similar to the case of obtaining all active 
>> sessions from one context using the manager inside the servlet code 
>> (findSessions). If someone have this piece of code (or similar), it will 
>> serve me.
> 
> 	To do something like that you need to do it yourself.  i.e. store a
> HashTable in the application context, then add and remove sessions to
> it when people login and logout, or at some other convinient time.
> Of course, this assumes your app isn't running in a distributed environment.
> If it is, then it's impossible to get a session object from a different
> JVM.  (well, you could serialize it and transfer the data, but it won't
> be the same object and it won't be useful for much other than extracting
> some info)
> 
> eric
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 
> .
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Java Web Parts -
http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by Eric Haszlakiewicz <er...@swapsimple.com>.
On Fri, Jun 02, 2006 at 11:15:03AM +0200, Carlos Alonso Vega wrote:
> Bob,
> 
> Thanks for the reply. My problem is that the session info I need is not 
> of the session that makes the request. I need to check other sessions in 
> the same context.  I can use the request because it is in the same 
> context, so the manager is the same.
> 
> This servlet is called internally from the app, and checks if some 
> session exists. It is similar to the case of obtaining all active 
> sessions from one context using the manager inside the servlet code 
> (findSessions). If someone have this piece of code (or similar), it will 
> serve me.

	To do something like that you need to do it yourself.  i.e. store a
HashTable in the application context, then add and remove sessions to
it when people login and logout, or at some other convinient time.
Of course, this assumes your app isn't running in a distributed environment.
If it is, then it's impossible to get a session object from a different
JVM.  (well, you could serialize it and transfer the data, but it won't
be the same object and it won't be useful for much other than extracting
some info)

eric

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by Bob Hall <rf...@yahoo.com>.
Carlos,

The javadoc indicates that the methods are deprecated,
but it looks like you could use:

HttpSession mySess = request.getSession();

// getSessionContext() is deprecated
HttpSessionContext sessCtx =
mySess.getSessionContext();

// getSession(String) is deprecated
HttpSession otherSess =
sessCtx.getSession("otherSessId");

-Bob

--- Carlos Alonso Vega <lo...@unavarra.es> wrote:

>  Thanks for the reply. My problem is that the
> session info I need is not 
> of the session that makes the request. I need to
> check other sessions in 
> the same context.  I can use the request because it
> is in the same 
> context, so the manager is the same.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by Carlos Alonso Vega <lo...@unavarra.es>.
Bob,

 Thanks for the reply. My problem is that the session info I need is not 
of the session that makes the request. I need to check other sessions in 
the same context.  I can use the request because it is in the same 
context, so the manager is the same.

This servlet is called internally from the app, and checks if some 
session exists. It is similar to the case of obtaining all active 
sessions from one context using the manager inside the servlet code 
(findSessions). If someone have this piece of code (or similar), it will 
serve me.

Regards

Carlos


Bob Hall escribió:

>--- Carlos Alonso Vega <lo...@unavarra.es> wrote:
>
>  
>
>>I am trying to obtain data about a session inside a
>>servlet, the code 
>>seems to be simple as
>>
>>localSession =
>>Ctx.getManager().findSession(sessionId);
>>
>>My problem is that I do not know how to obtain the
>>Ctx object from a 
>>request (HttpServletRequest)
>>
>>Thanks in advance
>>
>>    
>>
>
>Carlos,
>
>To obtain the HttpSession for the current request:
>
>HttpSession sess = req.getSession();
>    OR
>boolean create = false;  (or true)
>HttpSession sess = req.getSession(create);
>
>HTH,
>Bob
>  
>















---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Retrieve certain session data inside a servlet

Posted by Bob Hall <rf...@yahoo.com>.
--- Carlos Alonso Vega <lo...@unavarra.es> wrote:

> 
> I am trying to obtain data about a session inside a
> servlet, the code 
> seems to be simple as
> 
> localSession =
> Ctx.getManager().findSession(sessionId);
> 
> My problem is that I do not know how to obtain the
> Ctx object from a 
> request (HttpServletRequest)
> 
> Thanks in advance
> 

Carlos,

To obtain the HttpSession for the current request:

HttpSession sess = req.getSession();
    OR
boolean create = false;  (or true)
HttpSession sess = req.getSession(create);

HTH,
Bob

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org