• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Client/Server communication while JSP execution

 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Wanted more insight on the subject.
Consider a situation, where you open a DB connection, in the JSP.
This JSP page also has code to destroy the connection instance that it creates.
The user has requested for this JSP page.Assume that the connection over the network is pretty slow.The user gets frustrated and hits the stop button.We see that the JSP page stops loading.What I am curious to know about is what happens to the Connection (and other objects!!) that we create/intialize in that page!!
It would be helpful, if you could answer my following queries:

  1. Does the jsp page (servlet) finish processing the _jspservice method?
  2. Does the compiled JSP/Servlet destroy itself or will the destroy() be called immediately?
  3. Is the memory used from the page released?
  4. Will the page halt like it does, when a <jsp:forward> tag is encountered?

  5. Thanks in advance,
    ------------------
    Sandeep Desai
    [email protected]

    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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that HTTP is a request/response protocol. The user (web browser) submits a request, and the server (JSP container) reads the request, calls a servlet (your compiled JSP) to handle the request, and pipes the output back to the client. Your JSP is unaware that the client has disconnected until it tries to write the response. Also recall that a servlet (or JSP) does not shut down after a request is processed; it remains in memory read to accept more requests. You need to handle the database connection yourself.
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
Website: http://www.philhanna.com
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, if you close connections in the destroy() method of jsp, it wont be closed until the JSP is destroyed. ie the JSP will be in memory to service hundreds of hits and then when it finally gets destroyed by the server, the destroy() is called. Not after every request.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It really has nothing to do with JSP process. As long as a request is issued, the engine will start to process the request. What you hit on the stop button in browser just mean that the browser will not take the response from the server no matter if the response is issued or not. To the server once the request is received, no difference will make if the browser do not take the response.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add my little bit... the user pressing STOP on the client will break the HTTP connection. Your JSP will notice this when it is outputting HTML and the JspWriter object attempts to flush its buffer. An IOException will be thrown, and if you are not closing the connection in a finally clause, the connection will remain open until finalized (Bad Thing).
- Peter
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanks for the responses.
I guess, from your replies that the Container handles each client as a separate Thread (It may not be exactly like that, as it is going to depend on the implementation of the Servlet/JSP Container).
Considering that the threads are seperate for the client, another client would not suffer, if one of them hits the stop button, as destroying the Servlet/JSP is the responsibility of the container.
However, does this also mean, that such Connection Objects which the clients open (ofcourse, it is a very bad programming practice!!) would continue to hog the memory resources till the JSP is destroyed by the Container, incase there is no finally clause in my JSP client?
Also, may I request the group members to share their views on :


Will the page halt like it does, when a <jsp:forward> tag is encountered?
AND
Can we say that the JSP/Servlet would finish processing the _jspservice()/service() method, when the user clicks the stop button?


Thanks in advance.
Regards,


------------------
Sandeep Desai
[email protected]

  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
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Desai Sandeep:
Hi all,
I guess, from your replies that the Container handles each client as a separate Thread (It may not be exactly like that, as it is going to depend on the implementation of the Servlet/JSP Container).


You can safely assume that.

However, does this also mean, that such Connection Objects which the clients open (ofcourse, it is a very bad programming practice!!) would continue to hog the memory resources till the JSP is destroyed by the Container, incase there is no finally clause in my JSP client?


Not sure what you mean - who is opening these Connection objects? The normal HTTP connection between client and server is managed by the servlet engine (or HTTP server) and will be cleaned up automatically. If you open your own connection in your JSP, and do not close it in a finally clause, you will have a potential resource leak.

Will the page halt like it does, when a <jsp:forward> tag is encountered?


As mentioned, an IOException will be thrown. Assuming that you don't explicitly handle this then, yes, the page will halt.
- Peter
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,
Thank you for your response.


Not sure what you mean - who is opening these Connection objects? The normal HTTP connection between client and server is
managed by the servlet engine (or HTTP server) and will be cleaned up automatically. If you open your own connection in your
JSP, and do not close it in a finally clause, you will have a potential resource leak.


Yes, I had a code which didnot use Connection Pooling and was banking on the fact of opening JDBC connection objects in the JSP.For what you have posted, I understand, if I donot have a finally clause, there could be some serious memory leaks, depending on the web traffic.
Thanks once again for your views.
Regards,

------------------
Sandeep Desai
[email protected]

  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
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic