File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Current Request Object outside a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Current Request Object outside a Servlet" Watch "Current Request Object outside a Servlet" New topic
Author

Current Request Object outside a Servlet

Scot Meyer
Greenhorn

Joined: Feb 17, 2004
Posts: 12
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Scot Meyer
Greenhorn

Joined: Feb 17, 2004
Posts: 12
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

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

Joined: Feb 17, 2004
Posts: 12
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

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

Joined: Feb 17, 2004
Posts: 12
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

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

Joined: Feb 17, 2004
Posts: 12
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

Joined: Feb 17, 2004
Posts: 12
This is the fix for above.
Add to Server.xml
<Resources cached="false" />
Voila! Works fine now...Something about reading the !@$@!# manual...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current Request Object outside a Servlet
 
Similar Threads
get HttpServletRequest from java class
Singleton in a servlet
ServletInputStream class error
Problem in accessing an attribute through action elements
returning user defined objects