Servlet-Midlet communication with POST
Hi. I've two basilar questions:
1) I want to write a MIDlet that communicates with a servlet with POST.
I send two parameters using output stream like this:
dos.writeUTF("user=" + userName);
dos.writeUTF("&password=" + password);
dos.flush();
In the doPost method of the servlet i want to read the two parameters. Can i do that using request.getParameter("user") and request.getParameter("password")?
or I've to read that using the InputStream gained from the request object?
2) After the response, i want the MIDlet to send another data to the servlet but I get an exception saying that i'm trying to write data after the end of the request. Is there a way to do that?
Thank u.
# 1
1
You can use the HttpConnection interface as in this example
http://www.java2s.com/Code/Java/J2ME/HttpConnection.htm,
2
It depends on how you do the post. If you use a proper Java class to do it, that class will format you post using the http RFC allowing you yo use request.getParameter("user") and request.getParameter("password"), orherwise, if you just write a stream, you won't have any change on using the methods above, you will have to parse the stream youself.
MeTitus
# 2
Ok, so this is the code i use in the MIDlet to post the two parameters, but i can't get them in the servlet. What's wrong?
HttpConnection c=(HttpConnection)Connector.open(serverURL);
System.out.println("Connecting");
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
dos=c.openDataOutputStream();
dos.writeUTF("user=" + userName);
dos.writeUTF("&password=" + password);
dos.writeUTF("\r\n");
dos.flush();