How to run task in the background in web application? - java

I have a submit button in a JSP. This button calls 2 functions: function A(…) and function B(…). The latter sends emails to many users, maybe 1000 users or 2000. When I click on the submit button, the page keeps loading until the emails have been sent to all of the users. This takes too much time.
Is there any way to make the page load just until function A has finished and then run function B in the background? or does anyone have any other suggestion to solve this problem?

Make it work by AJAX calling two servlets or JSPs. When you click the button it does an AJAX call to servlet_A, and when you get the response back from there and put the output in a DIV or something, then make an AJAX call to servlet_B and let it take however long it will.
Contrary to what was said in a comment above, AJAX would be good for this because it runs asynchronously. You send off the request and whenever the response comes back, you do something with it or don't.

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.

what is the simplest way to call a java method on a button click in jsp page

I have some Java classes that have methods in them and when I click some of my buttons in jsp page a method should be called in order to do some operations. I tried servlets, I tried making an object and I don't know what to do anymore...Nothing works. Please help me !! Thank you !
There are two ways to do this.
Either submit form to make synchronous request to server to perform the action you want
Or you can invoke an ajax call for asynchronous call.

Strange behaviour with Navigate().back()

I'm facing an issue about the back function in selenium.
When I call it, it does nothing but if I call it twice, it works perfectly.
I've tried manually on my web browser and one back button is enought to go back.
Someone know why?
Thanks in advance!
The behavior might be a result of a scripted page with some code for auto-refreshing, suppressing or overriding the back button event /action.
So you might:
debug the page and stop on any JavaScript execution
and ask for a correction/change if you are in control of the page
avoid the back-button-action from Selenium-framework, that simulates browser back navigation
use instead a given button (if available)
implement this "feature" in your own framework as a workaround to click it (always) twice to ensure successful back-navigation

How to restrict double click on button in struts /Java?

I have created a web form. On Click of button, database query would be fired. The Problem is that when user clicks on button twice, query would be fired 2 times. I want to prevent that. Any help?
Take a look here
Struts2 has a built in mechanism for stopping double form submission that works on the server side instead of the client. You may need to add the TokenInterceptor if its not on the defaultStack you're using.
Here is a quick tutorial
If you are happy to use jQuery you could consider using
http://jquery.malsup.com/block/
onclick or onsubmit call $.blockUI();
Some Demos for your reference
http://jquery.malsup.com/block/#demos
The belt and braces approach is to set a JavaScript flag variable when the button is clicked and block subsequent submits "onclick". Then, on the server, implement the "synchronizer token pattern" (you can just Google that term to find out about it).

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