I have a Java servlet from which I need to send few notifications to the HTML page before I send the final response (which is a PDF file).
Basically I need to keep the user updating on steps I am performing while generating the PDF for them.
I guess, one way could be set various attributes using HttpSession and have them printed using JS. But I don't know how to do that dynamically without loading the page again.
Any idea on how can this be done? I am kind of new to JSP and servlets.
There are two ways:
AJAX polling - you send ajax requests periodically, and the server responds. The page does not get refreshed. Check jQuery for an easy way to make ajax calls
Comet - that's an umbrella term for what you are trying to do. Usually it means keeping an open channel with the server. There are many ways to implement comet (including the option above), but in Java, on the server side, there are two things to check:
Servlet 3.0 asynchronous support
Atmoshphere - chooses the best underlying mechanism provided by the container, which includes the async support above
You should probably look forward to use some implementation of COMET (with js library of choice).
If your html code is allowed to use html5 features, you could also use websockets
Related
I am relatively new to spring. I would like to know if there are any possible ways to automatically poll the database using Spring 4. The user would not need to refresh any pages for the code to fire.
I would need my jsp page to automatically poll the database every few seconds.
I think you are confused about how JSP works. JSP is a graphical facade on Java Servlets, which (normally) take in an http request, and returns some html or an error code. That's the servlet's entire life span... the servlet will re-run with every incoming request.
To do what it seems you want to do, you will need to find a solution in JavaScript / JQuery. A commenter has suggested you look up on AJAX programming - you should take his advice.
I have AngularJS application already in production and I need to make it Google-friendly. I have read about Ajax crawling mechanism and I have following two problems:
1. Since I have backend written in Java, I have tried HtmlUnit to make static snapshots, but it didn't work well with AngularJS. How can I serve snapshots of my AngularJS pages using Java?
2. As I have mentioned, application is already published and it uses simple hash without !. E.g.: /#/about, /#/home. Is it possible to keep this scheme? Change to /#!/ would require modifications of all links and would break all existing links (posted on web).
Thanks in advance!
The SEO is always an issue with single-page application.
I suggest you make a quick implementation of Phantom.js to display already rendered page to google bots. Check out this link for more informations.
Phantom.js will be a gateway to which you redirect every indexing bot request, it will then render your app like a normal user will do, and then send back the rendered page to the bot.
Also, it would be better to change your /#/ to only /, it's better for your users and also SEO. You just need to redirect every request to the index.html page and to use '/#/' as fallback for old browser which doesn't support pushState.
You also have some paid solution like https://prerender.io/ which works beautifuly.
I am making a automation website to send multiple files to an another site to prevent filling form every time to send a file.
I want to to make the POST request from server, because AJAX doesn't allow request to other domains.
How I can make this?
I am using Spring MVC3
Use apache http components - it allows you to perform http requests. You can also use (without 3rd party libraries) new URL(..).openConnection(), but it's less pleasant to code with it.
You can use Apache HTTP Components to implement pretty much any HTTP calls you want in your application. Also, note that it is possible to do cross domain AJAX with certain helper technologies like Flash ... if you really needed too.
I have a database from which I want to expose data.
Ideally I would like to be able to just add a URL into some other web page and that URL would then call the correct datum using the web app I use to interact with the database.
Would a web service be the best option?
Looks to me like a perfect job for ODATA:
The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.
See it action (showing query results in a browser is just one way to use ODATA).
A URL-based solution as you describe would only work if:
a) the web app framework you use can resolve the URL automatically as it parses and sends the HTML to the browser, or
b) the browser resolves the URL (e.g. the IMG element)
If the web app framework you use can resolve the URL (or if you can extend it so that it does), then you still need something that listens at that URL and retrieve the correct element from the database.
The approach here depends on whether you are doing Ajax style web pages or simple HTML, where each UI update refreshes the whole page.
The latter, a traditional page by page web site, it probably the simplest thing. For this explore JSP technologies. The idea is that you write what looks like an HTML page, but embed in it references to Java objects (or even Java code). In this case you should read up on simple frameworks such as Struts. The broad-brish idea is that you get this sequence of processing
Request arrives from Broswer, interpret it to figure out what the user wants to see
Some Java code talks to the Database gets data puts it in a Java Object
A JSP is chosen, that JSP picks items from the Java Object we just prepared
The JSP renders HTML which is sent to the Browser
In the case of Ajax, JavaScript in the Browser decides to display some data and calls a service to get it. So here, yes a "Web Service" of some kind is needed. Usually we use REST services, which return a payload in JSON format, effectively the data is transferred as JavaScript.
There are plenty of libraries for creating RESTful Web Services, for example Apache Wink.
I've coded some algorithms in Java and I need to include those algorithms in my web application.
I'm using Apache Tomcat and what I need to do is that when, for example, I click on the start button on my web page a Java class should be executed. I think this done by using servlets, am I right? If so, do you know where I can find some literature about it? I've searching in internet but it's a little bit confusing for me.
Yes, you're right. You'd want to write a servlet that handles request to an URI.
Here's some introduction:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html
Tomcat comes with some samples, you might look at the source code as a start, they should be in the webapps/sample directory.
The Tomcat documentation is also a good start.
http://tomcat.apache.org/tomcat-6.0-doc
I would check out this introduction to servlets
A servlet receives an HTTP request (e.g. a request for a page etc.). It will process that request via the methods doGet() or doPost(), and return an HTTP response. That response will contain your results (most likely an HTML page, although that's not mandatory).
I would (in order)
get a static 'hello world' page going
get a static page returning some data from your libraries
get a dynamic page returning some data from your libraries using data from the HTTP request
Integrating your library will be trivial, since a servlet is simply a Java class that can instantiate/call on any other Java class.
You will need to learn how to use Java in a web server. I would recommend using JSP just to start with as it allows you to create web pages that call Java code easily.
There are many JSP tutorials on the net - I believe this one is suitable: http://www.jsptut.com/