This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending post parameters to a servlet

 
Gar Morley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a game that that can transmit a high score to a servlet running in Tomcat. But I can't seem to figure out how to get the midlet to transmit a parameter using the post method.

I have a simple html form set up to test the server which sends two params by POST, the first called 'action' and the second called 'payload'. (The servlet uses it's request.getParameter("action") or ("payload") in it's doPost() to retrieve the parameters). This works fine, but how can I send params from midlet in a way similar to the html form?

I've tried writing 'myHttpConnection.setRequestProperty("action", "some-action")' in the midlet and then 'myOutputStream.flush()' when I've set properties, but the server doesn't seem to be getting anything.

I've also tried setting the content-length property to "x-www-form-urlencoded" but still no joy

Anyone have any ideas about how I should be sending params via post?
[ April 10, 2005: Message edited by: Gar Morley ]
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"GET" is the default request method for an HttpConnection object. So you'll need to change it like this:



Hopefully, that'll do the trick!

- Jeff
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
I ususally attach it to the URL string with ? and &

like

www.somesite.com/someServet?action=someAction&payload=somePayload


Yeah, but that's the "GET" method. When using POST, the url doesn't have any parameters tacked on the end of it. Instead, the parameters are send in the actual body of the http request, and are therefore invisible (usually) to the web-browsing user. Other advantages to using POST include:

  • Larger amounts of data possible. You're not constrained by any URL length limits that the browser or web server might have.
  • Non-ASCII (i.e., 7-bit) data possible.
  • The embedded data is not bookmarked. This is especially handy when the action does something you don't want to necessarily repeat (like purchase an item, or delete a record from a database).


  • - Jeff
     
    sun certified
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If doing more than a once off remote call to save high scores, try looking at project https://mermi.dev.java.net/ (mimics rmi for midp1.0 phones).
    Allows you to follow principals of having remote session interface, and a client stub to that interface. Hides the underlining communcation messing with http connection.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic