aspose file tools*
The moose likes JSP and the fly likes scriptlet processing query in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "scriptlet processing query in JSP" Watch "scriptlet processing query in JSP" New topic
Author

scriptlet processing query in JSP

Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Hello,
I am having a requirement from the client.

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.

Any other way to do it?

Thanks,
Vidhya
[ July 24, 2008: Message edited by: Shriya Kishore ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61001
    
  65

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "JSP issue" in a forum completely dedicated to questions on JSPs isn't very helpful.

You can go back and change your post to add a more meaningful subject by clicking the .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61001
    
  65

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.

The tactic of putting up a "please wait" notice while the long process is running using JavaScript has been discussed many times.

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 ]
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
yes it is.
I have to get the data to populate the report.

Any suggestions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61001
    
  65

Suggestions for what? You really haven't stated what you're looking for.

Are you unsatisfied with your current approach? If so, what do you want to change? What types of suggestions are you looking for?
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
ok....
I am currently having an asp report which is doing what I have stated above.
I need to replicate its functionality in JSP.

I would like to know if there is any other way of doing it apart from showing a progress bar and then hiding it to show the table of records.

Thanks
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4175
    
  21

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.


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61001
    
  65

Adjust the buffer (which, as pointed out, may have limited effect). Show nothing. Show a stopwatch. Show a ticking time bomb. The question is, what do you want to see?

For long-running processes I will:

a) Make sure that I'm not forcing too much data on the user in the first place. Paging and filtering techniques loom large in this step.

b) If there is still a chance for a delay, I display a simple animation to let them know that somethings afoot (perhaps something like this):

Loading...


Note that many browsers will not show animations between page loads. That's one of the reasons I usually employ Ajax to make such requests.
[ July 24, 2008: Message edited by: Bear Bibeault ]
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Hi Steve,
I am not quite understanding your approach though I find it appealing.

Can you please explain a little in detail?


Thanks,
Vidhya
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4175
    
  21

Originally posted by Shriya Kishore:
Hi Steve,
I am not quite understanding your approach though I find it appealing.

Can you please explain a little in detail?


Thanks,
Vidhya


Hmmm? I am just waiting for Bear's quote to finish loading, hold on a minute...

Oh wait, ....


Which approach do you want more info on? Using a concurrent class to read data (which I would prefer) or the zero buffer and constant flushing?
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
concurrent class as well as the zero buffer thing also.

I mean...I have never come across that so would like info on both of them.

Thanks a lot....
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4175
    
  21

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 ]
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
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.

Thanks once again.

Vidhya
 
Don't get me started about those stupid light bulbs.
 
subject: scriptlet processing query in JSP