Communication servlet to jsp without client request - java

I'm working on a project where we use Tomcat 8 as a application server and hence JSP and Servlets. Each JSP displays a kind of project list. The user of the application has the possibilty to simulate a project. What this means is not that important. When the user clicks the 'simulate'-button, the following process will be performed:
Via Ajax (javascript) a servlet will be executed which produces a zip file and stores the file on the hard disk of the server
a 'simulation'-flag will be set in the database for the respective project
a external java program (at the backend) listens for the 'simulation'-flag and if a flag for a project is set, the external program grabs the zip file stored by the web application
the external java program then runs the simulation for this project.
when the simulation is done, the external java program stores a new zip file on the hard disk of the server, sets a 'hey, I'm ready'-flag in the database and calls a servlet of the web application
this servlet saves the information of the data in the new zip file to the database
finish
So my question is: What is the best way to inform the JSP that the simulation is done?
The JSP still displays that the project is in simulation progress. My current idea is the following:
I could periodically send a Ajax request to the server to check the database for the 'hey I'm ready'-flag and if it is set, I could display the result on the JSP. But I've got the feeling that there are smarter solutions.
Maybe I could use the last servlet call (see point 5) to somehow inform the JSP that the simulation is done? The problem is that this servlet is not called by the client but by the external java program.
Do you have any suggestions? Or is there any technology I should read about?
Thank you.

using asynchronous ajax calls means that the function will return when the servlet finishes it's job and returns some HTTP code (200 for success).
so this example:
$.ajax({
url: Config.serverUrl+'/simulationServlet',
type: 'GET',
success: function(result) {
$('#infoBox').text("simulation is over");
}
});
will show that the simulation is done.

Related

Java Spring: Real-time status update to the client over REST API

I am developing a web application in Java Spring where I want the user to be able to upload a CSV file from the front-end and then see the real-time progress of the importing process and after importing he should be able to search individual entries from the imported data.
The importing process would consist of actually uploading the file (sending it via REST API POST request) and then reading it and saving its contents to a database so the user would be able to search from this data.
How could I show the real-time progress of this process? I found a tutorial for jQuery, which shows the progress of amount of data uploaded/transferred, but as the most the work is done while processing the uploaded file, I thought I would like a solution where before the line processing I find out the amount of lines in the file and then the user could see a live message like:
Lines processed: 1 out of 10000
It could update/change incrementally, but as one line is processed pretty quickly, showing each number of lines processed is not that important.
Either way, the question is, what's the easiest way to send these messages from Spring REST API to the client?
I found a solution myself and used Web Sockets for that.
I used this approach from the Spring documentation:
https://spring.io/guides/gs/messaging-stomp-websocket/
It could help on sending the messages for each processed line to the front end listener (after the web socket topic/connection is started) but I used a different approach for the data import, I used batch insert so that was unavailable for me, but web sockets are capable of doing that.

how to send file objects to servlet using json

I am new to web programming. My web application can upload files (uploaded by drag and drop method in javascripts ) and i want to retrieve them in servlet using Json . servlet file only needs the contents of the text files to do the calculation.
Can any one suggest how to do this ?
softwares used - netbeans ,tomcat
Thank you.
I'm not quite sure if you mean rendering your files in servlets or actually downloading them from your browser. I'm going to assume you mean rendering them. If so, then what you have to do is set up a URI which is associated with the content you want to render. Let's say this is a simple "hello world" rendering in the browser. what you would do is set up a URI as such:
localhost:3000/helloWorld.html.jsp
What you do on your back end is then wait to receive a http GET request to "/helloWorld.html.jsp" page, and then have your router send over the correct page. Luckily, with tools such as tomcat, the routing from local host is straight forward enough, and as your comments mentioned this is simple enough to do with the ample resources on the web.
Also an important point, you don't need JSON when using servlets (if i understood your problem correctly). If you want to send over data in a JSON format, then all you would do is modify the above steps to listen for "/sendJSON.html.jsp" (this could be an empty jsp), and you send over JSON from your back end in your response.

Periodically call of a Servlet from jsp page (Java)

I have a problem and i don't know how to resolve it...
I have 2 .dbf files with ~10000 records (geospatial informations) that i want to read from a jsp page, every time i read 200 records i have to send them to a servlet in some format (i haven't decided yet).
The Servlet must save every record as a Document object in Google Appengine (the limit of 200 records is specified by Appengine api's).
I can't upload the file to server and read server-side cause of some AWT classes not supported by Appengine, then i tried to read the files client-side and send to server the parsed records but i don't know how i can do this.
Someone have a solution to this problem?
jsp, once complied, is nothing but a servlet. That being said, you say:
I can't upload the file to server and read server-side cause of some AWT classes not supported by Appengine
Once you finish reading 200 records, why not insert it into the appengine from the jsp itself. The jsp will run on the server either way.

Handling Post Requests

yesterday i started brainstorm for a project of mine and I'm not sure if its the correct approach.
On my website I'm having an (kind of an order form) which sends a post to a target URL, which works with a simple curl php script. The target is an external service (where I have no access no rights, nothing). I only know that I will get a POST with further processed data back from the service, which I have to save into my DB.
in steps:
Users fills out the (order) form and posts data to an external url on my website.
data gets externally handled and after finishing that resents a post.
read incoming post data.
save data into DB.
Success page on my website.
My thoughts were to handle the incoming data with a servlet (spring maven project) but I'm not sure if this is a correct approach. Is there a better why to do this. Or is the first step with the php scripts wrong. thx for any help.
A simplest workflow could be
1. Forward the initial (Order form with values) request to a servlet
2. Invoke a post request using java to an external url inside this servlet (Using Apache http client, or libraries such as HTMLUnit)
3. Once you get the incoming response in your servlet, you can update your database.
If you are using spring, the controller could forward initial request to a business class which will handle this post processing and delegate the database update to respective DAO.
There are a number of suitable ways to handle this, and the decision is largely a matter of preference and what you're familiar with. Spring can handle this sort of job quite well.
Note: Maven is a build system for Java and some other JVM languages. I recommend using it, but it's not part of Spring; what you're probably looking for is Spring MVC.

Loading resources before running java code in servlet

I have a servlet in tomcat. It takes a really long time for the java code in the backend to execute. Is there a way to load static resources (css,images,javascript) in parallel with the code in the backend? Right now, they are only loaded once the code finishes running.
You could use an Ajax-style solution where you paint your page without data, with a placeholder for retrieving the data, maybe even with a "loading" spinner graphic.
The way that an Ajax call works, when the page is loaded, some Javascript will fire that will launch an Ajax request to Tomcat via XmlHttpRequest that will start the calculation. The browser will notify the browser when the tomcat request is completed. Then there will be some javascript in the webpage that will take the response and replace the placeholder. If the server returns an HTML fragment, it's as simple as executing in javascript placeholder-div.innerHtml = your-response-text.
Here's a basic tutorial on Ajax and a Java-based example that has the web front-end communicating with a Java Servlet back-end.

Categories

Resources