Yes, open URLConnection and write the POST request using code below from any web or stand-alone application.
URL url = new URL("http://localhost/servlet/Target");
UrlConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Send POST output.
dos = new DataOutputStream(urlConnection.getOutputStream());
String content = "arg1=" + URLEncoder.encode("name of the argument")
+ "&arg2=" + URLEncoder.encode("another name");
dos.writeBytes(content);
dos.flush();
Yes, but only in servlets
Yes, use <jsp:post> tag
No, POST requests cannot be sent from the server side
No, it?s impossible as there is no such classes in Servlet API
You can achieve mimic a browser only from Java servlet or Java applet providing code below:
URL url = new URL("http://localhost/servlet/Target");
UrlConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Send POST output.
dos = new DataOutputStream(urlConnection.getOutputStream());
String content = "arg1=" + URLEncoder.encode("name of the argument")
+ "&arg2=" + URLEncoder.encode("another name");
dos.writeBytes(content);
dos.flush();
Yes, create an instance of HttpServletRequest and call sendTo() method
|