Java Ajax Display Progress - java

I have a jsp page that calls a servlet when a drop down list with a bunch of different form names. If the user chooses to generate all forms the servlet then calls a java file that contains a loop. that runs each report generation one at a time. The whole process for this takes about 7 minutes as these are very large reports.
My question is, with keeping the same process (jsp -> servlet -> java code), can I get the progress status using ajax? What I want to do is find out what iteration the loop is on, find out if the function that writes the data out to excel (which is the format of all forms) has started, and write this information to the screen for the user to see so they are not guessing what is happening at any given time.
Additionally, we currently print out all this information to the console in eclipse so that for testing we can atleast know it is working. I am not sure if that matters.
Edit: I can provide code if needed but really the jsp contains a drop down list, the servlet takes that list and gets the value of the option selected (aka report1, report2, report 20...) and sends that value to the java code as a string. The java fucntion that is called is ReportAutomator.start(String reportNum){for loop here}. The for loop calls various functions to generate a report object which is forwarded onto a new java file that does more processing and then again forwarded to a final java file that unpacks the object and writes all the information to excel.
I am not really able to provide the actual code but I can try and make a dummy example if necessary. Sorry, and thank you for the help.

Determining the progress of a particular task is a surprisingly complicated thing once you get into the details however there is a jquery with ajax to update progress bar.
For a web app i recommend AJAX route 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

Related

Combine two http responses into one

Is it possible to send extra data attached to a http response via Java or Php?
My Website is a homework-platform: One User enters homeworks into a database, and all users can then see the homeworks on the website. The current load is very inefficient, as the browser makes two requests for eveything to load: One for the index file and one for the homeworks. For the homeworks request the client also sends settings of the user to the server, based on which the returned homeworks are generated by a Php script.
Now, I wonder, if it is possible, to combine those two requests into one? Is it maybe possible to detect the http request with Java or Php on the server, read the cookies (where the settings are saved), then get the homeworks from the database and send the data attached to the http response to the client? Or, even better, firstly only return the index file and as soon as possible and the homework data afterwards as a second response, because the client needs some time to parse the Html & build the DOM-tree when it can't show the homeworks anyway.
While browsing the web I stumbled across terms like "Server-side rendering" and "SPDY", but I don't know if those are the right starting points.
Any help is highly appreciated, as I'm personally very interested in a solution and it would greatly improve the load time of my website.
A simple solution to your problem is to initialize your data in the index file.
You would create a javascript object, and embed it right into the html, rendered by your server. You could place this object in the global namespace (such as under window.initData), so that it can be accessed by the code in your script.
<scipt>
window.initData = {
someVariable: 23,
}; // you could use json_encode if you use php, or Jackson if you use java
</script>
However, it is not a huge problem if your data is fetched in a separate server request. Especially when it takes more time to retrieve the data from the database/web services, you can provide better user experience by first fetching the static content very quickly and displaying a spinner while the (slower) data is being loaded.

How to make a call to controller multiple times to get chunks of data from the whole - Java, Spring MVC

I need to display the content of very huge log file (2GB - 5GB) on the browser window.
If I send the log file to JSP from controller and set the content to window.open from JSP, then it is a very big pain to load the page.
Or If I set the entire content of the log file to a StringBuilder in server Side(Controller) and send it back to JSP, then the page needs to be on wait for a whole time till all the log is set to StringBuilder.
So I want to send a chunks of data to the JSP in each call.
Example:
In the first call from JSP to Controller, i will send back first 20 lines
In the second call from JSP to Controller, i will send back next 20 lines
Or is there any other best way i can do this?
Please help me out in this...
What you probably want to do it setup a controller to take a size and start location
page 1 would be
http://www.site.com/_internal/logtail?characters=100000&start=0
page 2 would be
http://www.site.com/_internal/logtail?characters=100000&start=100000
You can then use the Random Access File in java to seek to the Nth location and read N characters. if someone wants another page, you increase the start location and keep going via ajax
http://docs.oracle.com/javase/tutorial/essential/io/rafs.html
(Side Note: I often use _internal to denote a protected url. This is something I setup on sites with the use of ACL's [Access control list] to protect that url so only a limited set of people can access it - I assume you probably don't want the public seeing your log files. If its a protected site anyway you may not need to worry)

Programmatically calling a PHP page and needing client-side Javascript to execute

I have a PHP page that grabs a variable via GET and then pulls in some information from a database based on that variable. Once finished with the server-side stuff, there is some javascript that runs and takes the data supplied and creates a .png image using a 3rd party API and then saves that image to my server using an AJAX POST call to another PHP page.
That all works fine, but what I'd like to do now is automate some calls to that PHP page. Namely, say I have 100 such variables to go through, I want to, preferably in Java with a for loop, call that PHP page with each variable in turn.
The problem is that client-side javascript. It won't execute with the simple URLConnection in Java. It seems like I need some sort of browser replicator or some way to have java act like it's calling the PHP in a browser?
Alternatively, I could make do with having a third PHP page act in place of the Java as the controller, but I'm faced with the same problem of getting the javascript to execute.
Am I missing something easy? Is this set up not possible? I'd really prefer to do it in Java if possible to fold it into other code I already have running.
Let me try to add some more specifics without bogging it down too much. There's a PHP file getData.php that takes in an ID number via GET. So I call it like ./getData.php?id=someId
That PHP file takes the ID, goes to my DB and retrieves some data and pastes it into the HTML source. Then once the page is finished, I have some javascript within getData.php that retrieves that data, formats it into a DataTable and passes it off to Google Visualization API in order to make a SVG chart.
Then I have more JS that runs that takes that SVG object, turns it into a Canvas object, grabs the base64 image data from it and finally POSTs to saveTo.php with the following array:
{'id' : id, 'data' : imgData}
saveTo.php simply takes in that POST data, creates a file on my server based on id and pastes the imgData into it. The end result is that I can pass in an ID to getData.php and end up with saved image of a Visualization chart that I want made based on data in my DB tied to that ID.
That all works by hand. But I have ~1,000 of these IDs and I'd like to have it so this whole process is run each morning so that I can have updated images based on yesterday's data.
I should mention that I did try using the 3rd party toolkit HtmlUnit (http://htmlunit.sourceforge.net/) but just keep getting these errors:
com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Some more searching around and hitting upon the right keywords to try finally led me to Selenium, which is exactly what I was looking for.
http://docs.seleniumhq.org/projects/webdriver/

Java HTML element reading

I am trying to create a java program that an detect changes in HTML elements on a web page. For example: http://timer.onlineclock.net/
With each passing second, the HTML elements of the clock change the source of the image they display. Is there anyway, using java, that I can EFFICIENTLY open a connection to this page, and be able to see when these elements change?
I have used HTMLUnit, but I decided that takes to long to load a page to be considered efficient enough.
The only way I know how to do it with a URL is to use a BufferedReader to read the page, and then use Regular Expressions to parse an HTML element within the source, but this would require me to "reload" the page every time that I want to see the properties of an element. Can anybody give me an suggetions on how I can detect these changes in a matter of milliseconds, without using much network resources?
Your best bet is to learn and use javascript instead of server-side java. Javascript program runs on the client side (ie: the web browser) as opposed to the server side.
Typical HTML document consists of elements (eg: text, paragraph, list items etc). With javascript you can create timer, action user's event accordingly and manipulate those elements.
http://www.w3schools.com/js/default.asp is probably a good introduction to javascript, I suggest you spend some time on it
The page in question appears to be a...Javascript digital clock.
If you want the current time, try new Date();.
If you want code to be called at a constant rate, try the Timer class. You can set a task to be called every second, which is the same frequency you will get by polling the page.
If you want to use the page as an external source of time, try the Network Time Protocol. http://en.wikipedia.org/wiki/Network_Time_Protocol It will provide much lower latency and is actually designed for this purpose.

Reading data for an output efficiently in Java?

I have a web application which runs on MySQL, and uses Java, Struts2 and JSP technologies.
There is an algorithm running on it and I want to test its performance (even seconds are important for me.) I will watch the output as a graphic on a page. I will get the information with Ajax and check for new information every second and write it to the graphic.
My question:
One of my classes produces an output for graphic input. What is the best way to communicate this. For example writing output to a database table and the other class will collect that value from there, or writing to a file, writing to database asynchronously (don't know how) and getting the value etc.
I need to do that operation with high performance because that algorithm uses system resources so much and should be run with best performance so I need a solution that doesn't affect my total performance.
Since you query every second, you're only interested in the latest state which means your algorithm can simply update this state as a variable in memory. The class that produces the output will have access to this variable.
I am presuming your graphic will be updated via javascript. Have the class that produces the output only produce output when something asks for it, by reading the value of the variable and returning it as a JSON string. In your case the Ajax request should query this class (every second). The class will return a JSON string which your javascript will have access to and will be able to use to update the graphic.

Categories