Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing post data from one java servlet to another

Tags:

java

servlets

I am trying to write a servlet that will send a XML file (xml formatted string) to another servlet via a POST. (Non essential xml generating code replaced with "Hello there")

   StringBuilder sb=  new StringBuilder();
    sb.append("Hello there");

    URL url = new URL("theservlet's URL");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();                
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Length", "" + sb.length());

    OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream());
    outputWriter.write(sb.toString());
    outputWriter.flush();
    outputWriter.close();

This is causing a server error, and the second servlet is never invoked.

like image 634
denny Avatar asked Sep 18 '08 20:09

denny


People also ask

How can we transfer data from one servlet to another?

The forward() method is used to transfer the client request to another resource (HTML file, servlet, jsp etc). When this method is called, the control is transferred to the next resource called.

How can we call one servlet from another servlet in Java with example?

You can call this servlet programmatically from another servlet in one of two ways. To include another servlet's output, use the include() method from the RequestDispatcher interface. This method calls a servlet by its URI and waits for it to return before continuing to process the interaction.

Can we use a servlet as a proxy for communications between two applets?

When used to support applets, servlets can act as their proxies. This can be important because Java security allows applets only to make connections back to the server from which they were loaded.

How do I get data in servlet which passed from HTML?

The getParameter() method is used to get the data from HTML file. The getParameter() method gives a servlet access to the parameter in its query string. It returns the parameter decoded value or null if the parameter is not specified.


4 Answers

This kind of thing is much easier using a library like HttpClient. There's even a post XML code example:

PostMethod post = new PostMethod(url);
RequestEntity entity = new FileRequestEntity(inputFile, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
like image 85
Peter Hilton Avatar answered Sep 27 '22 23:09

Peter Hilton


I recommend using Apache HTTPClient instead, because it's a nicer API.

But to solve this current problem: try calling connection.setDoOutput(true); after you open the connection.

StringBuilder sb=  new StringBuilder();
sb.append("Hello there");

URL url = new URL("theservlet's URL");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();                
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", "" + sb.length());

OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream());
outputWriter.write(sb.toString());
outputWriter.flush();
outputWriter.close();
like image 25
Sietse Avatar answered Sep 27 '22 22:09

Sietse


The contents of an HTTP post upload stream and the mechanics of it don't seem to be what you are expecting them to be. You cannot just write a file as the post content, because POST has very specific RFC standards on how the data included in a POST request is supposed to be sent. It is not just the formatted of the content itself, but it is also the mechanic of how it is "written" to the outputstream. Alot of the time POST is now written in chunks. If you look at the source code of Apache's HTTPClient you will see how it writes the chunks.

There are quirks with the content length as result, because the content length is increased by a small number identifying the chunk and a random small sequence of characters that delimits each chunk as it is written over the stream. Look at some of the other methods described in newer Java versions of the HTTPURLConnection.

http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html#setChunkedStreamingMode(int)

If you don't know what you are doing and don't want to learn it, dealing with adding a dependency like Apache HTTPClient really does end up being much easier because it abstracts all the complexity and just works.

like image 34
Josh Avatar answered Sep 28 '22 00:09

Josh


Don't forget to use:

connection.setDoOutput( true)

if you intend on sending output.

like image 28
Craig B. Avatar answered Sep 28 '22 00:09

Craig B.