• 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

(Urgent)Access HttpServletRequest from a class

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
...
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic