aspose file tools*
The moose likes Servlets and the fly likes How to Instantiate HttpServletRequest Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Instantiate HttpServletRequest Object?" Watch "How to Instantiate HttpServletRequest Object?" New topic
Author

How to Instantiate HttpServletRequest Object?

David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
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

Joined: May 10, 2001
Posts: 3244
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.


Dave
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
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

Joined: Jul 18, 2001
Posts: 236
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
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

Joined: May 10, 2001
Posts: 3244
David
HTH can mean:
hope that helps, or
happy to help
Were glad you found this site now dont be a stranger.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Instantiate HttpServletRequest Object?