JSF 2.0 Execute javascript on FacesContext.responseComplete() - java

I'm using JSF 2.0, and when a form is submitted, I am generating a download. As a result of this, the submission is NOT using AJAX. When the file has been generated for download, I am calling FacesContext.getCurrentInstance().responseComplete(); to tell the browser it's done.
However, for the sake of User Experience, I want to show a "loading" message on screen when the file is being generated, then remove this message once the download has been generated. Is there a way to do this considering the lack of AJAX?
Thanks,
Chris
Edit:
I am already calling the javascript to show the message onSubmit. My problem is then calling the Javascript to remove the message when the response has completed.

Raise your loading message before submitting the form.
FacesContext.responseComplete() does not send any response to the client. It merely sets a flag in the context to tell the JSF framework that no further lifecycle phases should be processed after the current one completes.

Related

How to maintain information from a database on a jsp page?

I'm writing some not very complicated web application using servlets and jsp but there's some issue I still couldn't resolve.
On the jsp page I've got multiple forms filled out with information retrieved from a database. Also there's several commands that modify this information (add, delete, submit, clear). If I click the "Add" button a request is sent to the servlet which invokes some of DAO methods and forwards the request to the same jsp page with updated "dishes list". It seems that all works just fine but what the hell should I do with the other forms (like the menu) cause this information is being removed after refreshing.
What is the best way to maintain all that information on the page? Should I send all that stuff with every command?
You can use Post/Redirect/Get pattern. More information can be found here: http://www.javacodebook.com/2013/08/20/post-redirect-get-pattern-in-spring-mvc/
Post
Initiate POST request with modified data.
Servet is processing request and updating database.
Redirect
Servlet is sending redirect to user browser.
Get
User browser is creating http get request.
Servlet is processing request and getting required data from database.
Response with latest data is sent back to the user.

Display HTML form only once in Tomcat Server

I have an tomcat server application where in a HTML form is displayed. Once I submit the data the page redirects to another page. In the back-end there is a JAVA program which takes some hours to complete execution. In the mean time if some-one tries to open my HTML page displaying form(First page) from another browser or another tab, it has to redirect automatically to the execution page. Is it possible to do this with Tomcat Server and JAVA? (I am using it on RHEL machine. So please do not post platform specific solutions.)
Here is an approach:
Introduce a flag in your server side code to determine whether the multi-hour-job is in progress
Whenever backend job is started, set the flag. When job done, reset the flag.
Either in your servlet doXXX method or in a servlet filter, check flag if set then redirect, if not then return the form view

Crawl contents loaded by ajax

Nowadays many websites contain some content loaded by ajax(e.g,comments in some video websites). Normally we can't crawl these data and what we get is just some js source code. So here is the question: in what ways can we execute the javascript code after we get the html response and get to the final page we want?
I know that HtmlUnit has the ability to execute background js,yet some many bugs and errors are there. Are there any else tools can help me with it?
Some people tell me that I can crawl the ajax request url, analyze its parameters and send request again so as to gain the data. If things can't work out according to the way I mention above, can anyone tell me how to extract the ajax url and send the request in correct format?
By the way,if the language is java,it would be the best
Yes, Netwoof can crawl Ajax easily. Its API and bot builder let you do it without a line of code.
Thats the great thing about HTTP you don't even need java. My goto tool for debugging AJAX is the chrome extension Postman. I start by looking at the request in the chrome debugger and identifying the salient bits(url or form encoded params etc.)
Then it can be as simple as opening a tab and launch requests at the server with Postman. As long as its all in the same browser context all of your cookies(for authentication, etc.) will be shipped along too.

Spring mvc result page and back browser button

On my MVC spring application I send a form to a page using post method to perform a search with some parameters. The results of the search is a list and, for every entry is possible to navigate to a details page. It works fine but, when an user try to come back to result page with back browser button (or a Javascript), I get an "Expire page" error. Refreshing the page, it rexecutes the post submit and it works fine.
To prevent this error I added
response.setHeader("Cache-Control", "no-cache");
to the search controller and everything works fine with Safari and Firefox but I get the same "Expire page" error when I try to execute the web application with IE (8 and 9).
What's the right way to go to a detail page and come back without getting any error?
thanks for your time!Andrea
The right way is to use GET instead of POST: searching is an idempotent operation which doesn't cause any change on the server, and such operations should be done with a GET.
Not expiring a POST request seems to undermine the very idea of a POST:
Per RFC 2616, the POST method should be used for any context in which a request is non-idempotent: that is, it causes a change in server state each time it is performed, such as submitting a comment to a blog post or voting in an online poll.
Instead, I would restructure how you view the detail records, maybe with an overlay DIV that doesn't refresh the underlying page that shows the results. With many modern web apps, using the BACK button can cause issues with page flow.

detecting if a file is ready and serving it for download

I have a Spring MVC web application that is generating a report on the server, once the report is generated, I need to enable a button that allows the user to download it. I am not sure how to go about doing this.
I figured that I will have to spawn off a thread that will just keep checking for the existence of the file and use javascript (jQuery or prototype most likely) to handle the UI elements, but I'm just not sure how to tie these all together.
There are no threads in Javascript. Instead you'll set a timeout to do the polling. The polling would take the form of a URL that will respond with some sort of "ready" indicator when the file is ready. If the file is not ready, then the AJAX success handler will start another timeout. When the server says it's ready, your Javascript handler will make the button visible and no further polling will be necessary.
Check this example here http://forum.springsource.org/showthread.php?t=70489 and let know if it works
You could use some type of messaging on the server that tells the client when the file is ready e.g. we us a table for all report requests and the server writes the status into the table and the client is then asking for the status of the report job with an AJAX call every few seconds.

Categories

Resources