Reputation: 7134
I would like to call a servlet from another servlet doing two things:
This is very easy to do from a form, but I need to do it from another servlet. Any ideas how?
Upvotes: 2
Views: 1542
Reputation: 4333
You can use java.net.HttpUrlConnection
or maybe Apache HTTP client to send a POST/GET request to the other servlet. You will basically be invoking the other servlet the same way a browser would.
Upvotes: 3
Reputation: 7215
It sounds like you want to send an HTTP POST with java. I would recommend using apache HttpClient. Check out this question Add parameters to Apache HttpPost
You can also do this with pure java with (HttpUrlConnection)[ http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html].
Upvotes: 0
Reputation: 22477
It sounds like request forwarding or include is what you're looking for. What you actually do will depend on what you intend to do with the output of the target servlet. Are you going to display it somehow? Or are you simply discarding it? You may in some cases, need to be a bit more "creative" in how you invoke those methods (e.g., either creating your own request/response instances, or wrapping the current request/response so that state changes are isolated).
Alternatively, to keep things simple you may want to just open a network connection to your target servlet's mapped URL as Jeff suggested.
Upvotes: 1