wood burning stoves 2.0*
The moose likes Servlets and the fly likes (Urgent)Access HttpServletRequest from a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "(Urgent)Access HttpServletRequest from a class" Watch "(Urgent)Access HttpServletRequest from a class" New topic
Author

(Urgent)Access HttpServletRequest from a class

Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Hi Dear friends
Inside the service method of a servlet I have instantiated class1. class2 is instantiated inside class1.
How can I get HttpServletRequest from class2?
Thank u for your reply.
Best Regards,
Pourang


Best Regards,<br />Pourang Emami
Dave Smith
Greenhorn

Joined: Jan 12, 2000
Posts: 23
have a function that from the httpservlet, you can pass itself to class1, which can pass itself into class2:
HttpServletRequest {
Dave Smith
Greenhorn

Joined: Jan 12, 2000
Posts: 23
Oops, Reply button got hit early
have a function that from the httpservlet, you can pass itself to class1, which can pass itself into class2:
HttpServletRequest {
...
class1.setParent(this);
...
}
Class1 {
...
public void setParent(HttpServletRequest x) {
class2.setParent(x);
}
...
}
Class2 {
HttpServletRequest main;
public void setParent(HttpServletRequest x) {
main = x;
}
...
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Watch out that your auxilliary class does not save a reference to the request. Request and response objects must be managed by the container and you should never keep a reference to them. Of course if your class1 and class2 objects are temporary and only used for one request it is not a problem.
Bill
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Dear friends Dave and William
Thank u so much for your answers.
But William what you mean by not saving the reference?can't we for instance save SessionId in some static variable and use it for the second request?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
I mean the reference to the actual request or response object. You can certainly save a copy of String data that the request carries, just don't try to use a reference to the request in a later request-response cycle. The servlet container may try to discard or re-use it.
Anything specific to a user that you want to save should be going into a session or your own equivalent of a session. Nothing specific to a user should be going into a static variable (unless you only have one user! )
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (Urgent)Access HttpServletRequest from a class
 
Similar Threads
problem in passing while loop variables from database
Accessing component from main class.... Help!
calling a method
Is that an association?
Calling constructors of inherited classes