Precise Java Google
Home  | J2SE  | J2EE  | Book  | Free Books  | Contact
Chapter 1  | Chapter 2  | Chapter 3

Recipe 1.3: Displaying progress message


Browser may take long time to display the results when the web server processing a complex user request.


A web page in your application may take time to be displayed depending on the time it takes to process the user request and on the response size. In certain scenarios, web pages can take more than usual time that the user is accustomed with. For example, processing web pages with the following operations may lead to long response time.

1. Searching data
2. Using distributed transactions
3. Accessing remote systems
4. Accessing legacy systems

If a request results in a long response time, then the browser does not display anything to the user and the user may leave web site without proceeding further. Ideally a user may wait 5 to 10 seconds for the response and then leave the website without waiting for the response. When we have limited hardware and software resources and to overcome this problem one option is to let user know their request is being processed instead of just hang up the user till the request is processed.

So how can we let the users know that their time taking request is being processed in the background?


You should display an intermediate message to the user while the request is being processed in the background and then display the results.

You might have seen 'Please wait….' message in some of the web sites when you browse through the Internet. The same concept is borrowed in this recipe but concentrates on implementation in Servlets and JSP. The idea here is to display a user-friendly message immediately after the request and before displaying the results. The following code describes how you can pass an immediate message to the user using JSPs. Note that there are many ways you can write the code to display the message. So, we encourage you to write your own code for progress message. Listing 1.3 shows starting page by which a user starts an action.

Listing1.3 Start page: startProcess.jsp

<% session.removeAttribute("statusBean"); %>
<jsp:useBean id="statusBean" scope="session"
class="com.cookbook.util.StatusBean"/> #1
<% new Thread(statusBean).start(); %> #2
<jsp:forward page="Progress.jsp"/> #3

(annotation)<#1 Creates StatusBean in session scope>
(annotation)<#2 Starts StatusBean thread>
(annotation)<#3 Forward to Progress.jsp page while StatusBean thread running>

Listing 1.4 shows the code of Progress.jsp where the user sees intermediate message for some time before seeing the page content.

Listing1.4 Progress.jsp

<jsp:useBean id="statusBean" scope="session"
class="com.cookbook.util.StatusBean"/> #1
<% if (!statusBean.isCompleted()) { %> #2
setTimeout("location='Progress.jsp'", 1000);
<% } %>
<% if (!statusBean.isCompleted()) { %> #3
<H1 ALIGN="CENTER"%>Please wait..while we process your request..</H1>
<% } else { %>
<H1 ALIGN="CENTER">Put your page content here...</H1>
<% } %>

(annotation)<#1 Gets previously created StatusBean>
(annotation)<#2 Checks whether the StatusBean's process is completed or not, if the process is not completed then refresh the page again after one second>
(annotation)<#3 Display intermediate message if the process is not completed>

Listing 1.5 shows the thread class that is used to process the user request concurrently while the intermediate message is displayed to the user.


package com.cookbook.util;

public class StatusBean implements Runnable, Serializable {
private boolean status;

public StatusBean() {
status = false; #1

public synchronized boolean isCompleted() {
return status;

public synchronized void setStatus(boolean status) {
this.status = status;

public void run() {
Thread.sleep(20000); #2
this.status = true; #3
}catch(InterruptedException e){

(annotation)<#1 Processing status is false when the StatusBean is created>
(annotation)<#2 Simmulates processing time for 20 seconds>
(annotation)<#3 Sets the status is true once the task is completed>


The starting page where the user makes a request is startProcess.jsp. This page starts a thread class and forwards it to Progress.jsp. Progress.jsp displays intermediate message first and checks whether the process is completed or not after every one-second by refreshing the page with JavaScript code. is a thread class that starts from the startProcess.jsp and runs concurrently with Progress.jsp. It allows checking the status with a boolean flag.

Make sure that you provide a user-friendly and interactive message to the user depending up on the process that is taking place in the background so that they know their request is being processed while they wait for the content.

Feed back

We appreciate and welcome your comments on this section. Email (remove ZZZ which is placed to prevent spam). Please note that we may not be able to reply to all the emails due to huge number of emails that we receive but we appreciate your comments and feedback.


Copyright © 2001-2005, Ravi Kalidindi and Rohini Kalidindi. All rights reserved.