Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet having trouble with POST data

 
Ben Wu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18214
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
u can try encoding the parameter name as well:
String content =URLEncoder.encode("param1") + "=" + URLEncoder.encode("foo");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic