aspose file tools*
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
Author

sending post parameters to a servlet

Gar Morley
Greenhorn

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

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

like

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

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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 &

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

    Joined: Apr 12, 2005
    Posts: 1
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: sending post parameters to a servlet