aspose file tools*
The moose likes Servlets and the fly likes How to give delay in JSP ? 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 "How to give delay in JSP ?" Watch "How to give delay in JSP ?" New topic
Author

How to give delay in JSP ?

Shivaji Bhosale
Ranch Hand

Joined: Mar 12, 2001
Posts: 70
Hi all,
In JSP, we have a bean which does a lot of computation & takes time substantially.
So here instead of letting user to wait for page to come up, we want to give 'wait'
message over page. & after computation is done, then need to display result.
The problem is, we cant give delay message, since HTML will be rendered only when,
processing with page is done.
Its like you know in our standalone applications for spalsh screen where at the time
of launch of program we purposefully give delay & proceed with further execution.
So the same way in HTTP scenario, does anyone have any idea.
Eagerly waiting for ur replies !
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
This is a very naive solution that could work. Here goes ...

Suppose you have a 2 page web application. The 1st page is a normal html page containing a form and the other page is generated dynamically by JSP (page2.jsp)
Thus, to create a delay, introduce a new JSP/Servlet generated page, that does nothing but, stores the results in the session and starts off the processing in a Thread, generates a unique ID for this request (session ID should be fine) and returns including a
Thus, the splash page is displayed for 10 seconds in the above case and then the browser makes another request to the jsp page, page2.jsp with the CODE.
In the jsp page, check if the Thread has completed processing (you need a server side communication mechanism for this which is described later)
If so, get the data and do the needful.
If not, Wait till the data is ready (synchronization). Thus, the browser will be still showing the splash while trying to connect to the new page.
Server Side Communication
As you will be spawning threads to do the work, you need to provide a means for the Threads to communicate completion of work. There are various ways you can do that. The simplest way is a Message Board, where each thread has its own space.
When the server hands work to the thread, put a mark in the thread's space (say FALSE).
When the worker finishes the work, it erases the mark in its space (TRUE) and so on...
Disadvantages :
I know there could be a lot of threads spawned at the server side that would affect server performance. But, if the expected server load is not too high, a thread pool could be used.
I hope this helps.
Ashwin.

[This message has been edited by Ashwin Desai (edited March 20, 2001).]
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
I have done this using javascript to. When the user does whatever is necessary to start the process- ie clicking a button or clicking on a link, take him to a page that says "processing". Now, on that HTML page, create a dummy form, with the action set to the page or servlet that does the actual processing. The "processing" page will stay up until the other page finishes all the processing and is ultimately displayed.
<dummy processing page follows>
...
PROCESSING
...
<form name="dummyForm" action="LongProcess.jsp" method="post">
<script>
document.dummyForm.submit();
</script>
</form>

HTH
Brian
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Brain,
You trick looks neat! I like it!
regds
maha anna
liz munroe
Greenhorn

Joined: Mar 01, 2001
Posts: 5
is this the flow of the problem?
1) page1.jsp loads
2) user clicks on something to go to page2.jsp
3) page1.jsp stays in the browser until page2.jsp does all it's stuff and then page2.jsp loads completely.
if so, make page1.jsp like this:
<html>
<head>
<script>
<!--
function showWaitingMessage() {
// IE5 and NN6
if (document.getElementById) {
document.getElementById("waitingpart").style.visibility="visible";
document.getElementById("realpart").style.visibility="hidden";
}
// NN4
else if (document.layers) {
document.layers["waitingpart"].visibility = "visible";
document.layers["realpart"].visibility = "hidden";
}
// IE4
else {
document.all.waitingpart.style.visibility="visible";
document.all.realpart.style.visibility="hidden";
}
}
//-->
</script>
</head>
<body>
<div id="realpart" style="visibility:visible">
Your page1.jsp normal html and/or other jsp stuff goes in here.
and
so does:
<a href="page2.jsp" onClick="showWaitingMessage()">
</div>
<div id="waitingpart" style="visibility:hidden">
Please stand by while the data is gathered etc...
</div>
</body>
</html>
BUT...
if page1.jsp is a form and the action is page2.jsp, change it to:

<form name="myform" action="page2.jsp">
...your form stuff here...
<input type="Button" name="thesubmitbutton" value="Submit" onClick="showWaitingMessage();window.document.myform.submit()">
</form>
or if it's the case that half of page2.jsp begins loading and then stalls while you perform the other stuff, let me know and I can try to help with that case instead. :-)
vetriselvi
Greenhorn

Joined: Mar 20, 2001
Posts: 4
Hi I have similar situation (as above)but bit different any help is appreciated.
1)login.html
2)validation.jsp(redirects to error.jsp or result.jsp)
3)result.jsp and error.jsp
when user enters his login info and submits it goes to validation.jsp there bean is giving validation info and based on that validation.jsp is forwarding to appropriate pages.
validation page does not display anything.
if he is valid user then other bean fetching more info and preparing result.jsp(approx after 11sec) and then result.jsp is diplayed.
Can some one help me to display some processing page in between.
Thanks
Vetri selvi
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
This is what i would have done but may not be the best.
your login.html call the validation.jsp on submit. Your validation.jsp calls a validation bean which contains a method which will return "true" or "false" depending on the success or failure of your validation. Now check the return value of that method inside your jsp. If it returns faLse throw an exception which will automatically take you to the error jsp page, if you have one defined in your jsp(or redirect it to error.jsp explicitly) else use <jsp:forward> tag to go to the next success page

Hope this helps.
Ajan
[This message has been edited by Ajan Balakrishnan (edited March 23, 2001).]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"vetriselvi",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
Don't get me started about those stupid light bulbs.
 
subject: How to give delay in JSP ?