Push data from Servlet to webpage - java

Ok .. I think this question is dumb but its something I need so asking.
I have a servlet that checks health status of about 20-50 servers depending on situations.
I am trying to reduce the time to as low as possible but it still takes me 15-20 seconds to respond.
I am printing stuff on console for my satisfaction that the servlet isnt stuch but a user cant see that.
Is there any way I could print data on the requesting or a new page so that user knows that server isnt stuck - I dont wanna pass the request as there is still some processing to do.
I just want to print like I do in console. I checked System.setOut(PrintStream) but this also caries the data only after I pass request dispatcher - not good for me.
I am sorry if its a dumb question. I couldn't find a solution for my problem. Any help will be appreciated. Thanks in advance.
UPDATE: I am currently displaying a picture that says loading. But even if the server crashes the picture is still there. In my case its not true indication of servers state.

I think what you are looking for is supported by WebSockets, https://www.google.com/search?q=websockets.
You'll have to update your javascript, assuming your browser supports it, and then update your servlet code, and container to support. Jetty supports it, I'm not sure about the other containers.
Or alternatively, you could save your progress to a table or something and poll that data.

With the first request you could show a page with some kind of "please wait" indicator and do a second request that actually fetches the data (eg. via JavaScript).

Show a loading bar using javascript . This would tell the user that something is happening in the background .

Related

How would I give immediate feedback in a JSP?

I have a JSP form, that (when the user clicks the "submit" button) instantiates a Java class, and calls a method on it. This method then submits a request (in a proprietary format) to a server running on an entirely different box. That back-end server then either sends the submitting user an email, and returns "SUCCESS" to the Java method, or it returns an error message.
Once the Java method returns from calling the program on the other box, it passes the result to the JSP, which either tells the user to expect an email, or displays the error message it got.
The problem is that this takes time to happen. And as the back-end server has evolved, and become more complex (it now has to call a web service running on a cloud server), that response time has gotten longer.
And now we have users who, because there's no immediate feedback, are either re-clicking the submit button, or refreshing.
Is there a way I can give the user some kind of immediate feedback, as soon as the JSP begins to process the "submit," that will be sent before the whole chain of instantiation, method call, remote system call, and so forth begins?
I found something on my own that will be good enough: I can do an "out.println()" followed by an "out.flush," and that will give immediate feedback. A quick-and-dirty test proves it out. And the fact that I wasn't already aware of it shows just how little I know about JSP (although I will note that it never came up in the first JSP tutorial I found, and it wasn't until I looked at other tutorials that I was even aware of its existence).
But I'd welcome a better solution.
Just as I always welcome constructive criticism.

JSP Progress Bar

Is there a quite easy way to implement progress bars in JSP. Basically I have a JSP page that goes to a servlet that calls up a method, now this process is going to run for a long time and I want to indicate the status of the progress just like the progress bar that shows up in the eclipse taskbar when we execute any java program.
I have found a nice tutorial here http://onjava.com/pub/a/onjava/2003/06/11/jsp_progressbars.html but it seems little outdated.
Are there any new and easy ways to implement this?
Determining the progress of a particular task is a surprisingly complicated thing once you get into the details. How do you determine for example that you are 50% done? And what happens if the last 10% of your task takes 1/2 the total time?
Usually for a web app, if you do want progress bars, then going the AJAX route is best, as some of the posters mentioned above.
I find, however, that on the web, it is suitable to indicate to the user the something is happening. Just have a spinner of some sort made visible when the page is submitted, and then hidden again when it is rendered (see here). This is very easy to do, causes no additional performance hit, and is indicative of some sort of progress
There is a lot of solutions for example jQuery.
You can use Jquery with Ajax to update progress bar:
http://jqueryui.com/demos/progressbar/
In my opinion, when you call your jsp, it should return to the user immediately but also indicate that some complex task is being dealt with in the background (with a loading spinner, for example). If you want to know the task's progress, you should use either Ajax polling, or Comet push in order to retrieve it from your server. For just getting and displaying progress, I think Ajax is sufficient and Comet might be a bit of an overkill. Here is more on the Ajax approach:
http://en.wikipedia.org/wiki/Ajax_(programming)
More on your question, a servlet(and also a jsp) work according to the HTTP protocol, which is based on the request->response model. In today's world, there are rarely sites where you go to a jsp to do a complex task and then you have to sit around doing nothing while your request is complete. You might want to give your user the freedom to still interact with your web-application/website while his request is being dealt with in the background.

Java Filter and count of calls

I have written test Filter (javax.servlet.Filter) and started to debug. And I was surprised that one refresh of html page calls twice method doFilter().
Could anybody describe me why it happens ?
Thanks.
Perhaps your filter was called also for static elements (images, etc.). Check your filter path declaration in web.xml.
One way to check what's really happening is to use either Fiddler or Firebug. Or both.
Another strategy to use is printing value of request.getRequestURL().toString() before doFilter(), so you can see what requests are being served. It's difficult to pinpoint why are you seeing 2 requests because the cause might be hidden somewhere in your environment or configuration.
Fire up Fiddler and watch the requests being made.

Action in fb:request form is always null

UPDATE: this has just started happening again, would appreciate any help.
I am using server fbml to render an Invite Friends iframe and it was working fine up until two days ago. Basically, after submitting the form, a java action should be invoked where I get all the id's of the user's invited.
However, I now notice that an "invite by email" popup is showing up and, most importantly, my java action isn't being hit at all. When I inspect the form in firebug, I notice that the action is always empty (though all other parameters are preserved correctly).
I've tried setting email_invite=false on the multi-friend-selctor but to no avail.
My code looks like the following:
<fb:fbml>
<fb:request-form action="http://localhost:8080/myAction.do?method=someAction" method="POST" invite="false" target="_top" type="invite" content="CONTENT_GOES_HERE" >
<fb:multi-friend-selector showborder="false" actiontext="Invite your friends to use our app." target="_top" email_invite="false">
</fb:request-form>
</fb:fbml>
</script>
Any help would be greatly appreciated on this.
UPDATE: this has just started happening again, would appreciate any help.
Thanks, gearoid.
Facebook have recently added an extra step into the multi-friend selector process, which is what you're seeing. There isn't, as far as I know, any way to stop this from appearing.
However, you should still be getting the result of the selector sent to you once the user navigates away from the "invite by email" popup.
Facebook's latest update can be seen in this status post.
To prevent that popup appearing add this attribute to the multi friend selector
import_external_friends="false"
Turns out this can happen a lot of different ways.
A) You can call FB.init multiple times
B) Call FB.init with the wrong key
C) You can supply a action that doesn't map to your current application on facebook
D) You or your staff can accidentially supply the same canvas/app url for two apps.
Most of of the solutions online point at A-C. When someone accidentally creates a second dev app pointing at localhost or 127.0.0.1 as the canvas url for multiple apps you admin you will get hosed. Took me hours upon hours to think to search our other apps. So i hope this saves someone a truck load of time. Cheers
I had an incorrect appId set in the fb-root javascript. This caused the error.

Get status of servlet request before the response is returned

Good evening,
I am in the process of writing a Java Servlet (Struts 2, Tomcat, JSP etc) which is capable of doing some fairly complex simulations. These can take up to 2 minutes to complete on the and will return a graph of the results. It is trivial to calculate the percentage of the simulation completed because the process works by repeating the same calculations 1000s of times.
I would be interested to know if anyone has ever tried to use client side technology to provide any estimate of the percentage complete. I.e query the servlet processing to get the number of cycles completed at various point throughout the simulation. This could then be displayed as a bar in the client browser.
Any thoughts, advice, resources would be much appreciated.
Thanks,
Alex
In your database, have a table to maintain a list of simulations along with their server-calculated progress.
In your web-application, use AJAX to query the server every few seconds (1-20 depending on load is what I'd go with) and update a graphical progress bar. Most javascript libraries have simple timer-based AJAX functions to do exactly this sort of thing.
There's a few details to figure out, such as whether or not completed simulations remain in the DB (could be useful logging info), but overall, this should be fairly simple.
You could encapsulate your response in a mime/multipart message and sends your updates until you have a full response done.
Bugzilla uses this in their search to show "Searching ..."-screen until the searchresult is done.
If you want to use plain Struts2, you should take a look at the ExecuteAndWait Interceptor.
It works by the old refresh-with-timeout method. Sure, it has lower coolness factor than some AJAX thing, but it's simple and works (I've used it).
Struts2 takes care (by using this interceptor) of executing the action method (which would typically take a long time) in a separate thread, and it returns a special result wait until the work is completed. You just have to code a jsp that shows some "waiting..." message for this result, and make it refresh to the same action, repeatedly, with (say) two or three seconds of timeout. The jsp has access to the action properties, of course, hence you can code some getProgress() method to show a progress message or bar.
AJAX is the way to go here.

Categories