If you want your connection to be alive for all the requests served by the servlet, you would need to initialize the Connection in the servlet init() method.
Although this technique would solve your problem, the threading issue would still be there.Hence, you would need to either use the synchronized block for accessing the Connection Object.
You may encounter performance problems , particularly,if your web-traffic is quite high.The best work-around for this would be "Connection Pooling", if the number of users are too many and unpredictable.
Hope this helps
Here is how i solve this current problem. I spawn a thread which reads data off the Inputstream from the socket. I have a synchronized block where i issue a wait method to wait so i won't leave the doGet() method. If my thread finishes execution i then notify to leave the doGet method this works fine. But i think it screws up some part of my servlet application. I am making a chat servlet where this servlet just reads the data from the socket. But when i connect another user in the other instance of the browser this one stops recieving messages from my server only the one that is in the other browser windows continues recieving. Could this be the problem with my approach ? Or is it caused that i am running both clients that access my servlet though same machine which is now trying to fight which one will recieve a respond from the server ? What i asked does it make sence ? if not i'll try to rephrase it. thanks.
Am sorry for interpreting your message incorrectly.I thought, you required information on DB connection.
To maintain a persistent connection with the client, as you have rightly mentioned, you would need to set an appropriate header.
I am quoting a paragraph, from Jason Hunter's book - Servlet Programming, where he discusses about HTTP Response Headers.It is available on Page 146, Table 5-2, in his book.
Connection Used to indicate whether the server is willing to maintain an open(persistent) connection to the client.If so,its value is set to keep-alive.If not,its value is set to close.Most web servers handle this header on behalf of their servlets, automatically setting its value to keep-alive when a servlet sets its Content-Length header.
Hope this helps,
- Sun Certified Java Programmer Scored 93 per cent
- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
- IBM Enterprise Connectivity with J2EE Scored 72 per cent
- Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
[This message has been edited by Desai Sandeep (edited April 10, 2001).]