• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Current Request Object outside a Servlet

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get the current request object within a class that is not a servlet nor a JSP page. I would like to create a static function where I can get the current Session,Request,or Response objects. Is there someway to do this by Thread.currentThread() or ServletContext? A simple example...
public class Current {
public static int UserID() {
HttpServletContext request = Current.Instance.Of.Executing.Servlet;
return (int)request.session.getAttribute("CurrentUserID");
}
}
So I could call...
int currentUserID = Current.UserID();
instead of doing a super long inline statement within my servlet or JSP code.
(int)session.getAttribute("CurrentUserID");
This is a very simplified version of what I am trying to accomplish as more complex versions would allow a object to be returned that may habe many getter methods.
[ February 17, 2004: Message edited by: Scot Meyer ]
 
Sheriff
Posts: 67746
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, there is no way you can pull it out of the ether. You could pass it to the object in the constructor and keep a reference to it. But I'd be very very cautious with that approach. It would make it very easy for the reference to the request to outlive the scope of the request itself. Be aware that a request is only valid while under the stack tree of the servlet service method. Storing a reference to the request and using it outside of the scope of the service method will result in bizarre behavior.
I'd just buck up and do it the old-fashioned way.
 
Scot Meyer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't the current call be still in the stack? For example If the executing servlet made a call to a method then the method would be part of the stack inline with the current servlet. Now passing the request would enable the method to see the current request but I would think that I should also be able to say who was my caller and what information do it have i.e request, response, etc. Sorry if this seems confusing but I am transitioning from ASP.Net to JSP so still learning, Yee haw!
 
Bear Bibeault
Sheriff
Posts: 67746
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
Welcome back from the Dark Side!
And if I'm understanding you correctly, passing the request around while the service method is active is perfectly safe and a common practice.
My caution was that one needs to be careful to make sure that a reference to the request goes out of scope when the service method does.
Let's say for example, that you store a request reference in a bean that you store in the session. Retrieving that bean at a later time and trying to use the reference to the now-out-of-scope request will lead to nothing but tears and heartache.
 
Scot Meyer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In .not I would have used HttpContext.Current.Session... but I dont see any static methods in any servlet or equivalent classes that let me access it in this way. I had originally though well if I used the JspFactory and got the current factory but this didn't work out because I needed a 'this' of course that is not allowed in a static context. Sooo... I thought well if there was a way I could get the current instance from the Thread.currentThread() by casting it or inspecting another memeber.
I guess what I am trying to say is their a way through Thread.currentThread() and casting to get the current response within the stack that I am functioning?
 
Bear Bibeault
Sheriff
Posts: 67746
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. The current request and response are passed to the service method (which in turn either passes them to the doPost or doGet of the servlet, or makes them available through the implicit variables of a JSP).
From that point on, the request and response need to be passed to anything that requires access to them. While this may seem constricting compared to .not, it actually makes for a tighter "chain of evidence" if you will.
Where along the chain of your code are you losing access to the request/response such that you need to try to pull them out of some context?
 
Scot Meyer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I was trying to avoid a huge Servlet body and employ some code reuse at the same time. I find myself quite often going to the same method to get information about a users detail or current information. I like these types of calls to be fairly small and atomic (to promote reuse). I have used the session object to store session information but am leary about the amount of overhead I want to add to this object (probably because I understand asp session state better than JSPs or would that be Tomcat's?
 
Bear Bibeault
Sheriff
Posts: 67746
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
Nothing prevents code reuse in this scenario. I've written a huge library of reusable components that keep my servlet bodies to a few lines of code. You just might need to think about it a bit differently.
Rather than creating components that can just snatch context out of what are essentially global variables, you need to make sure that the contexts (be it the servlet context, the request or the response) get explicitly passed. Again, while this may seem tedious at first, you'll find that it makes for tight, deterministic code.
[ February 18, 2004: Message edited by: Bear Bibeault ]
 
Scot Meyer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I think I understand given the pseudocode example below how would you tackle this or do I have it all wrong and go back to the manual?

Personally I like the Customer.Current(sess).getAddress method. Thanks for all your help this is clearing it up.
 
Scot Meyer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the fix for above.
Add to Server.xml
<Resources cached="false" />
Voila! Works fine now...Something about reading the !@$@!# manual...
reply
    Bookmark Topic Watch Topic
  • New Topic