Hi Ruslan, 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 Regards,
Ivanov, By 'connection' do you mean database connection? When I first read your post, I thought you want keep the connection made by the browser to be alive all the time. I don't remember exactly. There is a 'keep-alive' = true HTTP header setting available. regds maha anna
Joined: Feb 22, 2001
Hello , by connection i mean a connection to a requested servlet not a database. Maha could it be setHead("Connection","keep-alive") ? I can't find anything on it. 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.
Joined: Apr 02, 2001
Hi Ruslan, 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.