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
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.
Joined: Jan 09, 2002
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
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 ]
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
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
Joined: May 10, 2001
David HTH can mean: hope that helps, or happy to help Were glad you found this site now dont be a stranger.