Quantcast

HTTP Session in java class

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

HTTP Session in java class

TIENNOS
Hi everybody,

I am noob in grails .

We are trying to put an (existing) hibernate application on the web and we want to automaticaly get the user who is stored in the session.

I would like to know, if there is a way to retrieve the grails http session in a java class ?


Best Regards

Etienne
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: HTTP Session in java class

Tom Nichols
If you are doing a lot of work that is relevant or needs to be
isolated to a single session, you should use a session-scoped service.
 For example, if your user can have a shopping cart, your cart can
contain a list of items, and your controller can just say
"cartService.addItem item" and know that the items is being added for
the user whose session made the request.  This scoped dependency
injection works much better than the 'pull' style session handling
where you just grab random items out of the session by passing a
"username" or "cart" key.

Now, more relevant to your question, I believe there's a 'session'
object available to your controller, which you should be very careful
if you're passing that around to other classes (i.e. passing it to a
singleton service could be bad) if that same instance could be
handling other sessions as well -- that's sort of asking for disaster.
 You could create 'userService' which, upon login, populates the
username and other info into the services's instance variables.  Then
you can inject that UserService to controllers and other services that
might need such information.

Does that make sense?  Hope this helps get you on the right track.
-Tom




On Fri, Aug 14, 2009 at 10:11 AM, TIENNOS<[hidden email]> wrote:

>
> Hi everybody,
>
> I am noob in grails .
>
> We are trying to put an (existing) hibernate application on the web and we
> want to automaticaly get the user who is stored in the session.
>
> I would like to know, if there is a way to retrieve the grails http session
> in a java class ?
>
>
> Best Regards
>
> Etienne
> --
> View this message in context: http://www.nabble.com/HTTP-Session-in-java-class-tp24972381p24972381.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: HTTP Session in java class

TIENNOS
Thanks for your answer, but if service are singleton it will not work.

I don't use domain class, I use java persistence class and when I save a data I want to know which user save this data.

The connected user is in session.user but I don't understand how to retrieve the session.user in my java classes like sessionfactory for hibernate ?

Tom Nichols wrote
If you are doing a lot of work that is relevant or needs to be
isolated to a single session, you should use a session-scoped service.
 For example, if your user can have a shopping cart, your cart can
contain a list of items, and your controller can just say
"cartService.addItem item" and know that the items is being added for
the user whose session made the request.  This scoped dependency
injection works much better than the 'pull' style session handling
where you just grab random items out of the session by passing a
"username" or "cart" key.

Now, more relevant to your question, I believe there's a 'session'
object available to your controller, which you should be very careful
if you're passing that around to other classes (i.e. passing it to a
singleton service could be bad) if that same instance could be
handling other sessions as well -- that's sort of asking for disaster.
 You could create 'userService' which, upon login, populates the
username and other info into the services's instance variables.  Then
you can inject that UserService to controllers and other services that
might need such information.

Does that make sense?  Hope this helps get you on the right track.
-Tom




On Fri, Aug 14, 2009 at 10:11 AM, TIENNOS<etienne.mariethoz@gmail.com> wrote:
>
> Hi everybody,
>
> I am noob in grails .
>
> We are trying to put an (existing) hibernate application on the web and we
> want to automaticaly get the user who is stored in the session.
>
> I would like to know, if there is a way to retrieve the grails http session
> in a java class ?
>
>
> Best Regards
>
> Etienne
> --
> View this message in context: http://www.nabble.com/HTTP-Session-in-java-class-tp24972381p24972381.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: HTTP Session in java class

burtbeckwith
In reply to this post by TIENNOS
org.springframework.web.context.request.RequestContextHolder.requestAttributes.request.session

Burt

-----

Thanks for your answer, but if service are singleton it will not work.

I don't use domain class, I use java persistence class and when I save a
data I want to know which user save this data.

The connected user is in session.user but I don't understand how to retrieve
the session.user in my java classes like sessionfactory for hibernate ?


Tom Nichols wrote:

>
> If you are doing a lot of work that is relevant or needs to be
> isolated to a single session, you should use a session-scoped service.
>  For example, if your user can have a shopping cart, your cart can
> contain a list of items, and your controller can just say
> "cartService.addItem item" and know that the items is being added for
> the user whose session made the request.  This scoped dependency
> injection works much better than the 'pull' style session handling
> where you just grab random items out of the session by passing a
> "username" or "cart" key.
>
> Now, more relevant to your question, I believe there's a 'session'
> object available to your controller, which you should be very careful
> if you're passing that around to other classes (i.e. passing it to a
> singleton service could be bad) if that same instance could be
> handling other sessions as well -- that's sort of asking for disaster.
>  You could create 'userService' which, upon login, populates the
> username and other info into the services's instance variables.  Then
> you can inject that UserService to controllers and other services that
> might need such information.
>
> Does that make sense?  Hope this helps get you on the right track.
> -Tom
>
>
>
>
> On Fri, Aug 14, 2009 at 10:11 AM, TIENNOS<[hidden email]>
> wrote:
>>
>> Hi everybody,
>>
>> I am noob in grails .
>>
>> We are trying to put an (existing) hibernate application on the web and
>> we
>> want to automaticaly get the user who is stored in the session.
>>
>> I would like to know, if there is a way to retrieve the grails http
>> session
>> in a java class ?
>>
>>
>> Best Regards
>>
>> Etienne
>> --
>> View this message in context:
>> http://www.nabble.com/HTTP-Session-in-java-class-tp24972381p24972381.html
>> Sent from the grails - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
>

--
View this message in context:
http://www.nabble.com/HTTP-Session-in-java-class-tp24972381p25006238.html
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...