aspose file tools*
The moose likes Servlets and the fly likes keep connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "keep connection" Watch "keep connection" New topic
Author

keep connection

Ruslan Ivanov
Ranch Hand

Joined: Feb 22, 2001
Posts: 72
hi how do you set so that you can keep the connection alive in a servlet ? I want to always have a doGet to send response to my client.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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,

------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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
Ruslan Ivanov
Ranch Hand

Joined: Feb 22, 2001
Posts: 72
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.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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.

Hope this helps,
Regards,


------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

  5. [This message has been edited by Desai Sandeep (edited April 10, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: keep connection