Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing request object from outside a servlet  RSS feed

 
Eric Martin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get a reference to the servlet request but am not having any luck.

I need to get an attribute out of the request scope from one of my java files that is not part of the servlet.

Any suggestions?

TIA!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

Welcome to JavaRanch!

The very best thing you can do is extract the value in the servlet, and pass it to the other class as a method call or constructor argument. This decouples the other class from the Servlet API -- always a good thing to do.

Alternatively, if you want to ignore that good advice, then you want to pass the HttpServletRequest object itself to the other class, in the same way.

I'm going to move this to our Servlets forum for further discussion.
 
Eric Martin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. I have changed the way I am doing things and appreciate your input.
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest, I was reading thru some posts and spotted this one, I was curious why you say detatching data from the servlet when passing it to an object was so important.

I usually do this anyways as I have a "Data Cleaner" object that I pass all incoming data into and it returns a hashtable with that cleaned and trimmed data. But once in a while, when i dont have a bunch of data coming Ill pass the request object to the helper object, specifically if Im just slamming data into a db or something.

Can you elaborate on this topic? If there are ramifications Im not aware of Id like to know then so I can fix any potential problems before they occur. I handle several enterprise level sites that are just now starting to get traffic but may soon have tens of thousands of customers banging through the all the time.

Thanks for any info you can provide.
[ July 29, 2005: Message edited by: DC Dalton ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is important to remember that the request object in managed by the servlet container and is only valid during one request-response cycle. Trying to keep a reference to a request after the cycle is a VERY bad idea.
There is a handy request method - getParameterMap() that grabs all request parameters in a single call.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!