update java arraylist changes dynamically in html - java

I have a array list in java which contains notification messages which will be displayed in a html page.
Is there way that when the array list gets updated(i will update this array list from the listeners in smack chat library) the list in html also gets updated simultaneously without refreshing the page.
I am not interested in hitting the controller using any scheduler that will update the list every x minutes.I need the list to be updated simultaneously.
I have heard that we can use web sockets on this.I have no idea on this.Is that the way to go.If so kindly provide the tutorial for the same.

There are many technology to make chatting application over web browser.
HTTP server push - nothing but HTTP streaming, which uses WebSocket API and browser should support HTML5, server can push the message to the client.
Pushlet - it takes the advantages of the persistent HTTP connections. and communicate using that connection.
Long polling - pull the data using ajax and jquery or any ajax based technique.
Flash XMLSocket - when your browser does not support HTML5 that time this can be used.
I worked with same kind of application where , we developed all the technique depending upon the user browser support, we invoke the appropriate technique.
you can get more info about WebSocket here

Related

How to wait in a continuous loop in servlet?

I am developing a servlet based application. One situation is that a client requests some data from a database which is sent back in the form of html. The client will modify this data and then sent it back to the server. Now the twist starts. There is not a single client. So multiple clients can request the same data. So what I am doing is that when the first client makes a request, this request is stored somewhere so that when the next user makes the same request he is denied the data.
Now suppose the first user gets the data and 2nd is denied. Now while the first user is on the html page which allows him to modify the data. I want to send continuous javascript async post requests at a fixed interval to inform the server that the client is active.
At the server side I need a thread or something which can keep waiting in a loop for the javascript async requests and if the request is not received within the fixed time then the thread removes the saved request so that future requests to the data will be accepted.
I have searched the entire day and looked at things like async servlets, ServletContext listener and scheduledExecutorservice. I dont want to use scheduledExecutorService as it is invoked at app startUp which I dont want to do since this specific situation is a minor part of the code and to handle it I dont want something running all the time. I need some background service which keeps running even after the server has returned requested data.
Servlets won't fulfill your requirements, therefore you should use WebSockets.
As per my understanding, you are trying to push data from the server, therefore you need to a push architecture instead of pull architecture (Servlets are based upon pull architecture).
Java has native support of WebSockets
You can find several tutorials on how to use WebSockets in a Java Web Application.
Here is a link to a basic WebSockets Tutorial.
Hope this helps

How to implement a consolle in an HTML page?

Here's the problem:
I'd like to expose a page within a site which is able to report some log lines.
The site is a java spring 3.0 web application.
Theoretically there are two ways to get the job done:
1- the server pushes the lines to be logged whenever they are ready.
2- the client does a polling for new lines.
I'd prefer the first way but I really don't know if it is feasible or not.
I imagine the scenario as follows:
the client REQUESTs the "consolle page"
the server RESPONSEs such page
END TRANSACTION
the server REQUESTs (or what?) the updates...
the client... ?
And finally, what techonolgy suits best my requirements? I suppose JSP are not enough maybe some javascript?
I've implemented similar things in the past using timed polling with AJAX.
Your console page would run some javascript/jQuery that polls the server every so often via an AJAX request, and if it receives new data, appends (or prepends, whichever you like) it to your console box, or div, or whatever it is you're using.
Last I checked (which was quite a while back), this is how Facebook chat worked (though it's probably changed since then).
There are push implementations you could use (check out HTML5 Websockets, that might help), but AJAX polling is probably the simplest solution for something like this.

How to display server-generated messages in JSF?

I'm currently workin' on a simulation environment build with spring (webflow) and jsf (and primefaces). Within my environment messages are generated when some events are triggered by the server (for instance the "cash Position" of a player has changed). I want this messages to be displayed in my webapplication every time they are triggered.
I think I'll have to use someting like reverse AJAX, but I wasn't able to make it run within my application. Now I wanted to know if you see any other possibilities respectively techniques to acheive my goal.
You can do a constant AJAX poll using the components shown here http://www.primefaces.org/showcase/ui/ajaxPollHome.jsf.
In a typical Server/Client model a TCP socket connection would be the best approach because the connection is typically left open allowing the server and client to send messages back and forth to each other bidirectionally.
AJAX communication is essentially HTTP protocol and is by default a stateless Client Request/Server Response unidirectional model.
A custom polling AJAX component like the one linked from micfra is a good example of how to address this problem. A custom polling component can be built with AJAX and Javascript as well. Essentially the client needs to ask the server for status updates after so many intervals. If the server has a status update or new information, then using Javascript events one can update the information on the client as well as the server.

Which good ways exists to reload page by server for news popular website?

Which good ways exists to reload front page by server every time that new article placed by editor in front page for (news popular website like fox.com, cnn.com)?
Thanks
You are looking for some asynchronous client/server communication.
You can either periodically poll the server (query it every x time unit), can result in wasted queries (in form of HTTP requests) and subsequently waste bandwidth, if lots of polls find no new data.
Or use what has been dubbed as "server push" which leaves an HTTP connection open for the server to push the updates back to the browser, without polling.
GWT and GWT-RPC:
Since you are on Java, I recommend you have a look at GWT, will simplify your "AJAX" work with its GWT-RPC. In GWT you use Java language on both the server-side and the client-side (compiled to JavaScript), and will handle all the implementation details on the asynchronous communication under the hood.
To do the polling here, you can have a timer on the client-side (on the webpage) that will call a method that you have written on the server to give the data, can either be a String that represents some news, or even encapsulate it into a News class, with title, summary, body, and timestamp etc. The News class will then have to be in a shared (between client and server) Java package, so the implementation can be used on both sides without having you duplicating code.
GWT Comet:
This gwt-comet library provides an
efficient Comet implementation for
GWT.
The library implements Comet by
streaming messages over long lived
HTTP requests to minimise latency and
bandwidth requirements and maximise
the throughput. This is opposed to
many other implementations which use
polling or long polling techniques.
http://code.google.com/p/gwt-comet/
What you want is an AJAX call on your front page that queries the server for new articles every few minutes and if the server returns a new article the Javascript should then display it to the page.
The poor man's alternative to AJAX (sheesh, does every two-bit Web site have to be Web 2.0 these days??) is simply inserting an HTML refresh command into the page. This is documented all over the Web; I Google for it whenever I need it, and keep forgetting how.
It's dead simple and works even if the user has JavaScript disabled. On the downside, it does refresh the entire page.

Async data fetching from Java WebService

I'm implementing a client-server GPS application. Client side is a J2ME midlet that sends GPS location via HTTP/XML to a Java Webservice (Tomcat servlet). The servlet stores positions in SQL database. The other client app is a web browser that can login and see the actual position of midlet using Google Maps. This client is written using GWT. All is deployed on the same Tomcat container. Now I'm wondering how to dynamically update current position in webrowser Google Maps of the mobile so that the client can see how the mobile moves. How to do it in GWT - should I create a timer object in GWT client and asynchronously send HTTP request to server for getting the actual positions? Or is there any mechanism in AJAX/GWT to notify client (web browser) about the data update?
Thanks
Dominik
Regardless of all the talk about "push", the standard HTTP model is still one where the client has to ask the server for updates.
In a GWT app, you should use the well-supported Ajax functionality to request small-granular updates (polling, I guess) from the server at regular intervals (5 seconds, maybe?) and use the newly obtained information to update your map info.
There is a detailed article on the GWT incubator web site about Server Push and how to achieve it with GWT. From the article:
Explains Server Push, sometimes known as 'comet', and how you can achieve this with GWT.
Then, if you take a look at the comments, you can find interested related information and open implementations as gwt-comet, GWTEventService ...
I agree with rok.
If your deployment is going to be small enough to be able to support one permanent connection per web browser client go with Server Push/ Hanging RPC/ Long Polling or whatever you want to call it.

Categories