• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

servlet having trouble with POST data

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 28833
212
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
u can try encoding the parameter name as well:
String content =URLEncoder.encode("param1") + "=" + URLEncoder.encode("foo");
 
Beauty is in the eye of the tiny ad.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic