Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Passing/Accssing HttpSession to service layer

 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have couple of methods in my service layer, out of which some are being called from controller and rest from JavaScript(DWR(AJAX)).I can pass request object for those methods which are called by Controller in service layer. Now I want to access one session attribute in one of my method that is being called by AJAX. How can I get request object, hence the Session object in service layer?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "call by AJAX" represents a separate request to a servlet. What is handling this request and why cant it get at the session?

Bill
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I missed that that 'AJAX Call' is also a request to servlet. So you mean to say that I should get the current session or request in JavaScript(AJAX) and pass it to service layer. Am I correct?
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. What is it about your servlet, regardless of how it is "called", that makes you think that you have no access to the session?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need to include one more parameter in your method definition like HttSession session and you're done.

From DWR website,

It is possible to get access to the HTTP servlet objects without writing code that depends on DWR - just have the needed parameter (i.e. HttpServletRequest, HttpServletResponse, HttpSession, ServletContext or ServletConfig) declared on your method. DWR will not include it on the generated stub and upon a call of the method it will fill it in automagically.



I come across this easy solution from https://coderanch.com/t/120480/HTML-JavaScript/DWR-Session-management-security thread.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No. What is it about your servlet, regardless of how it is "called", that makes you think that you have no access to the session?


I guess you misread or I misconveyed my post. I meant to say access to session in service layer class and not in servlet.
 
Bear Bibeault
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"service layer class" is not a meaningful term.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:"service layer class" is not a meaningful term.


What should I have used?
 
Bear Bibeault
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something that describes what you are talking about. How about just telling us?

Without knowing how this class fits in with anything else, how are we supposed to hazard any suggestions?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll be more careful next time.
 
Bear Bibeault
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better yet, explain what you mean now. Why wait until next time?

If you mean a non-servlet class that's called by a servlet, you;d grab the session data while it's available in the servlet layer and pass it to the subordinate class.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear, actually I want to access session in non-servlet-class and as I have mentioned previously it would be called from JavaScript through AJAX(DWR) and not from servlet. So how could I get session object in that class. But I found the solution that I mentioned in one of my previous posts.

Obviously, we can pass session object from servlet to any other class.
 
Bear Bibeault
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does DWR not have a means to give you access to any of the container-managed context?

As this is more about DWR than JSP, I've moved it along to the Frameworks forum.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Does DWR not have a means to give you access to any of the container-managed context?
As this is more about DWR than JSP, I've moved it along to the Frameworks forum.


It does have. But I didn't know before posting question so I asked it in JSP. Sorry about that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic