简体   繁体   中英

Calling servlet from servlet

I would like to call a servlet from another servlet doing two things:

  1. setting the content type to "multipart/form-data"
  2. setting the method to "POST".

This is very easy to do from a form, but I need to do it from another servlet. Any ideas how?

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.

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 (eg, 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 .

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM