Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Instantiate HttpServletRequest Object?

 
David Sica
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to re-use some code which requires me to pass in a HttpServletRequest as a method parameter. Does anybody know how I go about instantiating and populating such an object with a couple parameters?
Thanks,
David Sica
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David
Where are you when you need to pass this object. If you are in an HttpServlet then you already have an HttpServletRequest object. Otherwise, I'm not sure you can explicitly create one on your own. They are normally created by the app container and passed into the service method of your servlet for you to use.
 
David Sica
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's the problem in that I'm not in a HttpServlet. It seems like there should be some way to create my own HttpServletRequest object that I can pass to the code I'm trying to re-use. Basically, the problem is I have a jar file from an associate that I want to use but the method I need to call only takes a HttpServletRequest and I didn't want to re-write his code to overload the method.
David
 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServletRequest is a interface which is implemented by the the web-container.This is done because such an object needs to be populated with the request parameters send by the client.Since this cannot be done by us,we cannot create a instance of type HttpServletRequest.
HTH,
Manjunath
[ April 12, 2002: Message edited by: Manjunath Subramanian ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in actuality you could write any class of your own that implemented the interface, but why would you?
It certainly sounds like you're trying to reuse a peice of code in a way that the original designer had no intention of it being used. That way lies pain.
What I find usually happens in these cases is that you end up spending more time trying to work around the limitations of trying to misuse a peice of code than it would take to refactor the original code so that the common, re-usable aspects of it can be shared more readily, or even to write new code from scratch that serves the exact purpose you have in mind. It can also lead to a fragile and unmaintainable mess.
I'd step back a moment and evaluate what you are really trying to do, and sanity check whether trying to reuse servlet code for non-servlet purposes is really the best course of action.
hth,
bear
 
David Sica
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your input. I agree with Bear's comments. I guess there was some "laziness" involved in not wanting to refactor or write original code. But I was also looking for a "quick" solution. I agree it's a good idea to decide whether it makes sense to reuse servlet code in a non-servlet context. I think I knew this all along but didn't pay enough attention to my gut feeling.
Obviously, I'm new to posting here so what does the salutation HTH mean?
Thanks,
David
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David
HTH can mean:
hope that helps, or
happy to help
Were glad you found this site now dont be a stranger.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic