This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The requirement is : I have a link.On clicking of the link,a session is opened for the user and I have to create a temp table for the current session.Then I have to insert data into it based on certain request parameters and then I need to select data from the temp table and display a table to the user.
Now,my question is: If I do the table creation and population as well as select in a scriptlet tag (<% %>) will the user be shown an empty page till the time the process gets done. Because sometimes the select can take upto a min to get exeuted.
An approach from my side was : have a timer kind of a progress bar in the JSP,do all stuff in the <% %> tag and when the select has been executed, hide the progress bar div and show the table div.
Originally posted by Shriya Kishore: Now,my question is: If I do the table creation and population as well as select in a scriptlet tag (<% %>) will the user be shown an empty page till the time the process gets done. Because sometimes the select can take upto a min to get exeuted [sic].
Regardless of where you do a long-running server operation this will be true. Not just when you break accepted modern practices and put Java code in scriptlets on the page.
You might also want to investigate sending less data to the page at a time. Is the amount of data you are sending really useful or required? [ July 24, 2008: Message edited by: Bear Bibeault ]
Joined: Jan 03, 2007
yes it is. I have to get the data to populate the report.
First it would be best if you did the hard work in a Servlet or better another class you can run concurrent to the JSP (I know I am reiterating but it is the truth).
That said, you can set the JSP page's buffer to 0kb with: <%@page buffer="none" %> Then as you scroll through the result set you could immediately begin to populate the table for each row. After each row call <% out.flush() %> and you will get a table that grows rather than a blank page. You will still have some buffers to fill (proxies and IE itself has a buffer that must be filled before it first displays data) but you will get a quicker reaction.
The concurrent class method makes heavy use of the classes in java.util.concurrent. It starts by generating a runnable unit (in my example it will be a java.util.concurrent.Callable, but it could be a java.util.Runnable as well) that loads the data from the database.
In the runnable unit of code you will fill each row into a concurrent collection. The best to use is a form of java.util.concurrent.BlockingQueue where you want to wait for the next value(s) to be added rather than viewing snapshots of the database.
This runnable code is kicked off and run in a new Thread, and a Future object is used to monitor its progress.
Meanwhile, the JSP gets a reference to the BlockingQueue and retrieves values from it as soon as they become available and fills the page. It knows all the items are retrieved when the Future object says it is done.
So to kick it off, I have a DataTransportObject (DTO) which stores values for a single row on my table. This makes transport easy and understandable. In my example we will be getting a list of People from the DB so my DTO is called Person:
I then have the interesting class. This is my DataAccessObject (DAO) which gets the People from a database. This is a really cut down version of a DAO but I wanted to just show you the important parts. This is the 'concurrent' class with the runnable block of code which does the work (this example it is public void call()). To run this class you do 3 steps: 1) Create a new instance of PersonGetter_DB 2) Call the getPeople() method to get a reference to the BlockingQueue of People 3) Call the startRetrievingData() method to begin the work and get a reference to the Future object for monitoring the progress
The code is as follows:
That is a lot of code, sorry... One last bit though, the JSP. The JSP in this case will create a new PersonGetter_DB object using the jsp:useBean tag. It then uses scriptlets to call the other two methods and to start a while loop which checks the Future object (monitor) to see if the DB is done. In the while loop it populates the table.
Here is the code:
Note that this JSP has the buffer="none" attribute for the page directive, and flushes the output after each loop, so it is pretty much both options I suggested earlier. I like this better because most of your work occurs outside the JSP which makes it easier to code, test, and debug (both the JSP and the DB access code).
I would like to fill most of the scriptlets with JSTL and EL if I could, and if I designed the DAO slightly better I could do pretty well, but there is no JSTL while loop that I am aware of, and using the Queues iterator in a forEach loop wouldn't work so well (would block forever at the end). Given a little motivation you could put this all into a custom tag and be done with all the scriptlets, but until then ...
Anyway, this isn't a definitive answer for a DataAccessObject. But it is a pretty good addition to one I think. You should search the net for Java DataAccessObject, there are good examples in a lot of places (for example look up BalusC's blog, he has a good one) [ July 24, 2008: Message edited by: Steve Luke ]
Joined: Jan 03, 2007
Thanks a lot for the really detailed solution.
I really appreciate your effort to help.
Will look into it more in detail and repost if any issues.