views:

67

answers:

4

we have a classic JSP + Servlets application and would like to show "Content is loading" sort of message when data in the page takes a while to load.

Is there an easy way to do this via JS?

Scenario:

  • Page1 (a.jsp) -> select drop downs -> click search //data is sent back to server for db
  • URL changes to (b.jsp), white page is shown, then data load after 30ish seconds

for those 30 seconds I want to show a spinner or some message.

adding ajax or jquery would require a design change which we can not do right now. Though the application already uses jQuery for other stuff but b.jsp is making the DB call from that page...

+1  A: 

AJAX is the way to do it. You'll need something on the server side to help you keep track of progress.

duffymo
A: 

Load your data (in b.jsp) with ajax request.

Make a JS function which starts on load, let it show your message ('30 seconds left') and then let it perform ajax request to load necessary data.

I recommend you to use some js-framework, like jQuery, it'll make things much simplier.

In order to handle ajax-request on the server-side you'll need to create one more servlet and map it to some url, like your_app_name/ajaxsupport. This servlet should return data in some convenient format (it can be plain text, or xml, or JSON or whatever else). On the client you'll process received data and show it.

Roman
+1  A: 

calling DB in jsp would be very much frowned upon. given the premise of the question, you could

//b.jsp

<div id="msg">data is loading...</div>

<% 
   out.flush();
   db.performanLengthJob(); 
%>

<script> $("msg").remove(); </script>

<p>Data is loaded!</p>
irreputable
A: 

A quick solution can be to use onbeforeunload JS event of your base page html body s.g. like

<body onbeforeunload="$("yourComponent").show();">
gubra
The `beforeunload` is only called whenever the client is about to close the window or to navigate away, not when the window is finished loading.
BalusC