0

I have a java web application wherein a servlet runs that takes time to load, and once the servlet is done a jsp page will be displayed with the data from the servlet that ran earlier.

Is there a way to display a message or a gif while the servlet is loading?

  • Show your code, can't know what you are doing wrong if you don't show it. – khaja firoz Mar 30 '16 at 08:38
  • Duplicate of: http://stackoverflow.com/questions/1964839/how-can-i-create-a-please-wait-loading-animation-using-jquery – tak3shi Mar 30 '16 at 08:54
  • @tak3shi: it is not really a duplicate. The other page is specifically about jQuery, when OP does not askl for javascript but for servlet. – Serge Ballesta Mar 30 '16 at 11:42
  • Servlet can't do it. JS can do it. So duplicate applies. Unless OP rather wanted an unhelpful answer like "No you can't". – BalusC Mar 30 '16 at 12:06

2 Answers2

0

JavaScript is the way to go, Servlet is server-side you know.

As long as I know, once browser submits a request we can't control what happens until next page is loaded.

So, either you handle lag before loading the new page, or you handle lag after page is loaded. Of course, it's hard to load a page that requires authentication, before authentication is done, so better keep on login page until it happens.

My suggestion is that you handle login with AJAX, always being careful with security. Instead of letting browser submit the form, your JS gathers login information and asynchronously sends to Servlet using AJAX. I don't know how to do it but it can be done.

While your JS wait for response, you create some animation to distract user. Once response comes, if it login succeeds, your JS loads the new page.

Dhiraj Thakur
  • 338
  • 3
  • 11
  • The OP does not ask about how to handle login. There is also no need to use Ajax, the OP just wants to display a message or gif. – tak3shi Mar 30 '16 at 12:10
  • 1
    Now it's clear that i need to learn javascript. thank you for the insight you've given me. – Jason Bourne Apr 05 '16 at 02:27
0

If a servlet needs time to be able to return a result, you must split it. You can then use Javascript, or redirections and for the latter with or without refresh delays.

Javascript is the more evident way: the intelligence is client side. So you:

  • prepare the request
  • inform user that you are launching a processing that will take time
  • send an async request to the server
  • [ possibly inform user of avancement of processing ]
  • display final result when the request has finished

But it is not the only way. You could also:

  • send the request to a first servlet that starts asynchronous processing and immediately returns a page informing user that processing could take some time
  • the page contains a button Advancement that query for itself and display information on advancement
  • when the request is completed, the advancement page redirects to a page displaying the final result

Ok, this is not very nice because user has to click on a button to see advancement, but you can use a slight variation using a refresh header in the advancement page that refreshes it automatically. The refresh is a non standard tag but is supported by the majority of browsers (ref). And you get a page that informs user on the advancement of long time processing without a line of javascript.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252