How to get a reference to the HttpServletRequest request object....
Hi,
In each jsp page you have access to a very important variable request(of type HttpServletRequest).
Is there a possibility to get a reference to this object inside of a static method of an user-defined class without being forced to send it as a parameter to that method?
Thanks.
[306 byte] By [
IMIAa] at [2007-11-27 11:50:45]

no.
I mean, if you want you could include a static attribute in your user defined type that you set before you call the method that needs it...
tsitha at 2007-7-29 18:33:50 >

And if I set that static attribute is not dangerous that another request might overwrite it 1 millisecond later?
Is there any way to be sure that if I set a static attribute of a user-defined class is only visible for my current request?
Thanks
IMIAa at 2007-7-29 18:33:50 >

> And if I set that static attribute is not dangerous
> that another request might overwrite it 1 millisecond
> later?
Yes, very much so.
> Is there any way to be sure that if I set a static
> attribute of a user-defined class is only visible for
> my current request?
Technically, you could synchronize access to that static stuff. This is probably a BAD idea unless you really want the other requests to wait.
If you need multiple pages accessing this, then don't make it static, make a instance of a class to hold the data and act on it. Or make the static method you call take all the parameters it needs to act on.
> And if I set that static attribute is not dangerous
> that another request might overwrite it 1 millisecond
> later?
>
> Is there any way to be sure that if I set a static
> attribute of a user-defined class is only visible for
> my current request?
>
> Thanks
Make it non-static?
Why are you wanting this method to be static if you also want visibility to be limited to the current request?
EDIT: yeah, what the above poster said.
tsitha at 2007-7-29 18:33:50 >

My aim is to have a class let's say RequestUtils. In this class I'd like to save references to important objects like the user which is logged in, an object where I keep all the rights the current user has and some more stuff I need in some more places later on.
What I'd like to do is to find an easy way to get all these references without taking the overload to send a requestUtils instance to all methods I call just because later on I might need this reference.
The static way was exactly what I wanted to have till I realized that all requests share the same memory location.
Is there any way to do this or I should really create an instance and send it to all methods calls? What's the best way to do it in Java?
Thanks
IMIAa at 2007-7-29 18:33:50 >

> My aim is to have a class let's say RequestUtils. In
> this class I'd like to save references to important
> objects like the user which is logged in, an object
> where I keep all the rights the current user has and
> some more stuff I need in some more places later on.
Sounds like a Session object to me.
> Is there any way to do this or I should really create
> an instance and send it to all methods calls? What's
> the best way to do it in Java?
>
> Thanks
You shouldn't have to write your own object to do this. Please explain why a Session is inadequate for the purpose.
%
1) My big problem was/is to easy get a reference to the session object somewhere in the my user-defined classes without being forced to send it as a parameter everywhere. I've tried to find something like the HttpSession.Current from the .NET but I couldn't get it.
I was thinking I can manage with the static memebers of this RequestUtils (very easy to access from my user defined classes) when I realized that the static way is not a solution any more because gets overwritten between requests.
2) Most of the objects are only valid for one request. I don't really need to store all of them in Session.
IMIAa at 2007-7-29 18:33:50 >

> 1) My big problem was/is to easy get a reference to
> the session object somewhere in the my user-defined
> classes without being forced to send it as a
> parameter everywhere. I've tried to find something
> like the HttpSession.Current from the .NET but I
> couldn't get it.
Your user-defined classes should not have to be coupled to the web tier that way. Terrible design.
Are you writing in Java or .NET?
> I was thinking I can manage with the static memebers
> of this RequestUtils (very easy to access from my
> user defined classes) when I realized that the static
> way is not a solution any more because gets
> overwritten between requests.
Yeah, not a good idea.
> 2) Most of the objects are only valid for one
> request. I don't really need to store all of them in
> Session.
That's not how sessions work. You get one for each user. You won't store all of them, just the ones pertinent to this user and session.
Your requirements are still pretty fuzzy. Can't design something based on what you've posted.
%
Hi,
have you had a look at the interface javax.servlet.ServletRequestListener? It might solve the problem for you.
Alternatively you could implement a javax.servlet.Filter for the web application. Within the doFilter method (that is automatically called) you could store the request wherever you want.
As for the service threads you could think of using ThreadLocal variables. But be aware of the fact that some implementations of web containers use thread pools rather than newly created threads for servicing requests.