Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(Urgent)Access HttpServletRequest from a class

 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a function that from the httpservlet, you can pass itself to class1, which can pass itself into class2:
HttpServletRequest {
 
Dave Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic