aspose file tools*
The moose likes Servlets and the fly likes servlet having trouble with POST data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet having trouble with POST data" Watch "servlet having trouble with POST data" New topic
Author

servlet having trouble with POST data

Ben Wu
Greenhorn

Joined: Jan 02, 2002
Posts: 3
I'm having trouble with a servlet that i'm writing that's designed to handle POST data from a java application.
I've managed to get the servlet to recognize GET data using URL("http://localhost:7001/test2?data=foo") calls, but when I tried the following code snippet (pretty much lifted from the java docs)
--------------------------------------------
url = new URL("http://127.0.0.1:7001/test2");
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
printout = new DataOutputStream (urlConn.getOutputStream());
String content =("param1=" + URLEncoder.encode("foo"));
printout.writeBytes(content);
printout.flush ();
printout.close ();
--------------------------------------------
I get nothing. I tried mapping doPost to doGet, but it appears that the servlet is ignoring the post data.
though i can temporarily get around using GET methods, i'm gonna be hosed when I try to transfer files. any ideas?
TIA
-ben
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

I believe the proper format for POST is in line format:
param1
value1
param2
value2
The the servlet should be able to read values with getParameter exactly as it does with GET. You might want to check the RFC that defines POST for the finer details.


Customer surveys are for companies who didn't pay proper attention to begin with.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
As I recall, you also have to use the setRequestMethod("POST") method, otherwise it defaults to a "GET".
When you do an openConnection with a URL that specifies "http" you actually get an instance of the class HttpURLConnection (it extends URLConnection)
There is where you find HTTP specific methods.
Bill


Java Resources at www.wbrogden.com
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you are manually building the GET and POST requests from a GUI app, check out the following for what they look like:

Regular: http://www.javaranch.com/ubb/Forum7/HTML/006990.html
Multipart: http://www.javaranch.com/ubb/Forum7/HTML/006987.html
Meng Tan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
Hi,
u can try encoding the parameter name as well:
String content =URLEncoder.encode("param1") + "=" + URLEncoder.encode("foo");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet having trouble with POST data
 
Similar Threads
Servlet Communication using java.net
Application and servlet communication
Post to Servlet from Applet doesn't work
How to recive / return cookies to server?
Cell Phone Authentication with HTTP protocol