Communication between Midlet and Servlet using Apache Tomcat
Hi team,
I developed a simple Midlet which made the following operations:
- Open an http connexion with Sevlet using Tomcat.
- Open DataInputStream and DataOutputStream.
- Send 2 Strings (Username and Passwd) trough DataOutputStream to Servlet.
- Servlet checks if the username and passwd are correct and answers Midlet through DataInputStream.
Until this step, everything is OK. After, when I try to send another String from the Midlet using the same DataOutputStream (you need to know that http connexion, DataInputStreamand DataOutputStream are always opened), I get the following exception:
detailMessage:java.lang.String="Write attempted after request finished"
Thanks in advance for your help.
[754 byte] By [
a.med16a] at [2007-11-27 9:21:51]

# 1
Hello.
Could you clarify please, why are you sure that http connection etc. are always opened?
For example, if look at this tutorial: http://www.novocode.com/doc/servlet-essentials/chapter2a.html
It says:
- "The Web Server closes the PrintWriter or ServletOutputStream automatically when a service call returns. An explicit call to close() is useful when you want to do some post-processing after the response to the client has been fully written. Calling close() tells the Web Server that the response is finished and the connection to the client may be closed as well."
It's standard scheme of http interaction:
--> open_tcp--> get_request--> post_reply-->close_tcp_automatically.
Regards,
jDee.
jDeea at 2007-7-12 22:15:55 >

# 2
Hi jDee
I抦 not sure that the connection is kept opened, I mean by 搚ou need to know that http connexion, DataInputStreamand DataOutputStream are always opened?that I don抰 make an explicit call to close() method.
My aim is to keep the http connection alive, because the Servlet needs to send with push mode some request to the Midlet. The Midlet handles the request and answers the Servlet.
the behavior of my application is the following:
DataInputStream dis=null;
DataOutputStream dos=null;
they are 3 modules: mobile client (J2ME), Mobile Server (J2ME), Coordinator (Servlet)
1-Server: open Httpconnection with Servlet
2-Server: open dis and dos
3-Server: send user & passwd trougth dos
4-Coordinator: check user&passwd
5-Coordinator: send response
6-Server:keep connection opened
7-Server: execut while(dis.avalaible()<1) {};
.
.
.
8-Client: send Request to coordinator
9-Coordinator: redirect Request to the right Server
10-Server: get request dis.read()
11-Server: handles the request
12-Server: answer the coordinator dos.write()
13-Coordinator:redirect the Response to the right Client
14-Client:get answer coming from Coordinator
Regards
-Med-