You are on page 1of 12

http://stackoverflow.

com/questions/96360/writing-post-data-from-one-java-servletto-another

Writing post data from one java servlet to another

up vote do wn vote 1 share [g+]share [fb]share [tw]


favorite

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.
java servlets

link|improve this question

asked Sep 18 '08 at 20:06

denny 8771816 75% accept rate feedback

5 Answers

activeoldestvotes

This kind of thing is much easier using a library like HttpClient. There's even a post XML code example: vote do PostMethod post = new PostMethod(url); wn vote RequestEntity entity = new FileRequestEntity(inputFile, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); int result = httpclient.executeMethod(post);

up

11

accepted

link|improve this answer

answered Sep 18 '08 at 20:13

Peter Hilton 7,0651850 feedback

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 vote do open the connection. wn vote StringBuilder sb= new StringBuilder(); sb.append("Hello there");

up

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();

link|improve this answer

edited Sep 18 '08 at 20:17

answered Sep 18 '08 at 20:12

Sietse

1,87821436
feedback up vote do connection.setDoOutput( true) wn vote

Don't forget to use:

if you intend on sending output.


link|improve this answer answered Sep 18 '08 at 20:10

Craig B. 16218 feedback

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 vote do specific RFC standards on how the data included in a POST request is supposed to be sent. It is not wn vote 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.

up

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#setChunkedStreamingMod e(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.
link|improve this answer answered Sep 19 '08 at 21:18

Josh

2,3761819
Was this post useful to you?

http://www.devx.com/java/Article/17679/1954

http://www.devx.com Printed from http://www.devx.com/java/Article/17679/1954

Send Form Data from Java: A Painless Solution


Sending multipart/form data from Java is a painful process that bogs developers down in protocol details. This article provides a simple, real-world solution that makes sending POST requests as simple as sending GET requests, even when sending multiple files of varying type.
by Vlad Patryshev

ending simple GET requests from Java is extremely easy (be it an application or a servlet), but sending multipart forms, like when you upload files, is painful. The former can be done in one method call that does all the underground work. The latter requires explicitly creating an HTTP request, which may include generating multipart boundaries while being careful with the exact amount and selection of newlines (e.g., println() would not always do the right job). This article provides a simple solution that will relieve the pain of sending form data (like POST requests) from Java in most real-world scenarios. The intention is to make POST requests as simple as GET requests, even when sending multiple files of varying type (archives, XML, HTML, plain text, etc.). GET and POST Requests The two main methods for sending form data to a Web server are GET and POST. This section demonstrates how each method works in three scenarios. (Do not try to reproduce these at home. The URLs and e-mail addresses are fake.) Example 1. GET Request The following form uses the GET method:

<form method="get" action="hi.iq/register.jsp"> Name: <input type="text" name="name" value="J.Doe"> email: <input type="text" name="email" value="abuse@spamcop.com"> <input type="submit"> </form> It performs the same task as requesting the following URL: http:hi.iq/register.jsp? name=J.Doe&email=abuse%40spamcop.com If you were using Mozilla 5.0, the following would be the HTTP request sent to the server: GET register.jsp?name=J.Doe&email=abuse%40spamcop.com HTTP/1.1 Host: hi.iq User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8, video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66Keep-Alive: 300Connection: keepalive

Example 2. POST Request Now, let's replace 'GET' with 'POST' in the form HTML tag:

<form method="post" action="hi.iq/register.jsp"> Name: <input type="text" name="name" value="J.Doe"> email: <input type="text" name="email" value="abuse@spamcop.com"> <input type="submit"> </form> In this case, the HTTP request sent to the server looks like this: POST register.jsp HTTP/1.1 Host: hi.iq User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126

Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8, video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300Connection: keep-alive Content-Type: application/x-www-form-urlencoded Content-Length: 36 name=J.Doe&email=abuse%40spamcop.com Example 3. Multipart POST Request If the form contains file upload, you have to add enctype="multipart/form-data" to the form tag. Otherwise, the file won't be sent:

<form method="post" action="hi.iq/register.jsp" enctype="multipart/form-data"> Name: <input type="text" name="name" value="J.Doe"> email: <input type="text" name="email" value="abuse@spamcop.com"> file: <input type="file" name="file-upload"> <input type="submit"> </form> This form will produce the following HTTP request when sent from Mozilla 5: POST register.jsp HTTP/1.1 Host: hi/iq User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8, video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------29772313742745 Content-Length: 452 -----------------------------29772313742745 Content-Disposition: form-data; name="name" J.Doe -----------------------------29772313742745 Content-Disposition: form-data; name="email" abuse@spamcop.com -----------------------------29772313742745 Content-Disposition: form-data; name="file-upload"; filename="test.txt"

Content-Type: text/plain test data with some high ascii: Como ests? -----------------------------29772313742745--

Send POST Requests from Java Using the GET request from Java is very easy. You just create a URL with the request and then get an input stream: InputStream is = new URL("hi.iq/register?name=J.Doe&email=abuse@spamcop.com").getInputStream(); In most cases, servers that process POST requests won't reject your GET request. Still, sometimes you'll have to POST instead of GET, particularly when you upload a file. Wouldn't having a solution like the form from Example 2 be nice? The corresponding Java request could look like this: InputStream serverInput = ClientHttpRequest.post( new java.net.URL("hi.iq/register"), new Object[] { "name", "J.Doe", "email", "abuse@spamcop.com", "test.txt", new File("C:\home\vp\tmp\test.txt") }); Unfortunately, so far no such solution exists. The Solution: ClientHttpRequest If you search the Internet, you will find some partial or complicated solutions for using POST requests in Java. The best commercial solution probably is JScape's HTTP(S) component. Its well-designed API covers everything specified in HTTP (see RFC 2068). Other solutions are either too weird and complicated (see Ronald Tschalr's HttpClient) or too literal to be useful (check out this post from JavaRanch). Because of this lack of free, simple solutions, I had to develop my own (download the source code). It is of moderate complexity and has an obvious interface that I hope is both simple and relatively universal. It can be instantiated from a URL String, URL, or an already open URLConnection. After a user creates an instance of ClientHttpRequest, the user can add request parameters and cookies. Cookies are an important part of a request, especially with servlets that use cookies to keep track of sessions. One can add parameters one by one, using the following: setParameter(String name, String value); setParameter(String name, File file); setParameter(String parametername, String filename, InputStream fileinput) Or set them all at once, using the following: setParameters(Map parameterMap) or

setParameters(Object[] parameterArray). The following method sets parameter as a string value or as a file depending on the argument type: setParameter(String parametername, Object parameterdata). This method works the same way for cookies. One can add cookies one by one, using the following: setCookie(String name, String value) Or set them all at once, using one of these: setCookies(Map cookieMap) setCookies(String[] cookieArray) After the request is ready, the post() method posts the request and returns an input stream that will contain the server's response, the same way as in URL.getInputStream(). For convenience, additional post methods are available: post(Map parameters); post(Object[] parameters); post(String[] cookies, Object[] parameters); post(Map cookies, Map parameters). The following group of static post methods does all of this in one fell swoop: InputStream InputStream InputStream InputStream serverInput serverInput serverInput serverInput = = = = post(URL post(URL post(URL post(URL url, url, url, url, Map parameters); Map cookies, Map parameters); String[] cookies, Object[] parameters); Object[] parameters).

All these methods let the user post a complicated form and receive an input stream in one expression, like the one in Example 2 at the beginning of this article. Answer to the Eternal Question Now you know how an HTML form (GET or POST) gets passed to the server as an HTTP request and how you can reproduce this behavior in your Java programwithout overloading it with protocol details. You could say this solution answers the eternal question: How can one call a servlet or JSP from another servlet or JSP? Vlad Patryshev is an R&D engineer at the Java Business Unit of Borland. He recently started the myjavatools.com project. Contact him by e-mail.

DevX is a division of Internet.com. Copyright 2010 Internet.com. All Rights Reserved. Legal Notices

http://www.discursive.com/books/cjcook/reference/http-webdav-sect-uploadmultipart-post

11.9. uploading files with a multipart post


Table of Contents

11.9.1. Problem
You need to upload a file or a set of files with an HTTP multipart POST.

11.9.2. Solution
Create a MultipartPostMethod and add File objects as parameters using addParameter( ) and addPart( ). The MultipartPostMethod creates a request with a Content-Typeheader of multipart/form-data, and each part is separated by a boundary. The following example sends two files in an HTTP multipart POST:
?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.MultipartPostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; HttpClient client = new HttpClient( ); // Create POST method String weblintURL = "http://ats.nist.gov/cgi-bin/cgi.tcl/echo.cgi"; MultipartPostMethod method = new MultipartPostMethod( weblintURL ); File file = new File( "data", "test.txt" ); File file2 = new File( "data", "sample.txt" ); method.addParameter("test.txt", file ); method.addPart( new FilePart( "sample.txt", file2, "text/plain", "ISO-8859-1" ) ); // Execute and print response client.executeMethod( method ); String response = method.getResponseBodyAsString( ); System.out.println( response ); method.releaseConnection( );

Two File objects are added to the MultipartPostMethod using two different methods. The first method, addParameter( ), adds a File object and sets the file name to test.txt. The second method, addPart(), adds a FilePart object to

the MultipartPostMethod. Both files are sent in the request separated by a part boundary, and the script echoes the location and type of both files on the server:
?

1 2 3 4 5 6

<h3>Form input</h3> <pre> sample.txt = /tmp/CGI14480.4 sample.txt {text/plain; charset=ISO-8859-1} test.txt = /tmp/CGI14480.2 test.txt {application/octet-stream; charset=ISO-8859-1} </pre>

11.9.3. Discussion
Adding a part as a FilePart object allows you to specify the Multipurpose Internet Main Extensions (MIME) type and the character set of the part. In this example, thesample.txt file is added with a text/plain MIME type and an ISO-8859-1 character set. If a File is added to the method using addParameter( ) or setParameter( ), it is sent with the default application/octet-stream type and the default ISO-8859-1 character set. When HttpClient executes the MultipartPostMethod created in the previous example, the following request is sent to the server. The Content-Type header is multipart/form-data, and an arbitrary boundary is created to delineate multiple parts being sent in the request: POST /cgi-bin/cgi.tcl/echo.cgi HTTP/1.1 User-Agent: Jakarta Commons-HttpClient/3.0final Host: ats.nist.gov Content-Length: 498 Content-Type: multipart/form-data; boundary=---------------31415926535 8979323846 ------------------314159265358979323846 Content-Disposition: form-data; name=test.txt; filename=test.txt

Content-Type: application/octet-stream; charset=ISO-8859-1 Content-Transfer-Encoding: binary This is a test. ------------------314159265358979323846 Content-Disposition: form-data; name=sample.txt; filename=sample.txt Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: binary This is a sample ------------------314159265358979323846-Each part contains a Content-Disposition header to name the part and a ContentTypeheader to classify the part with a MIME type and character set.

11.9.4. See Also

RFC 1867 (form-based file upload in HTML defines a multipart POST) can be found athttp://www.zvon.org/tmRFC/RFC1867/Output/index.html. Each part is sent using MIME, which is described in RFC 2045, Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies (http://www.zvon.org/tmRFC/RF2045/Output/index.html).

http://hc.apache.org/httpclient-3.x/apidocs/index.html? org/apache/commons/httpclient/methods/multipart/MultipartRequestEntity.html

File f = new File("/path/fileToUpload.txt"); PostMethod filePost = new PostMethod("http://host/some_path"); Part[] parts = { new StringPart("param_name", "value"), new FilePart(f.getName(), f) }; filePost.setRequestEntity( new MultipartRequestEntity(parts, filePost.getParams())

); HttpClient client = new HttpClient(); int status = client.executeMethod(filePost);

You might also like