File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java Micro Edition and the fly likes sending post parameters to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "sending post parameters to a servlet" Watch "sending post parameters to a servlet" New topic

sending post parameters to a servlet

Gar Morley

Joined: Dec 15, 2004
Posts: 14
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

Joined: Mar 29, 2005
Posts: 71
"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

Joined: Feb 05, 2001
Posts: 17276

I ususally attach it to the URL string with ? and &



Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Mark Spritzler:
I ususally attach it to the URL string with ? and &


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

    Joined: Apr 12, 2005
    Posts: 1
    If doing more than a once off remote call to save high scores, try looking at project (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.
    I agree. Here's the link:
    subject: sending post parameters to a servlet
    It's not a secret anymore!