I've created a website in JSP with Servlets.
Users can login and they all get a session attribute with their userid.
I created a page with a textbox and button where I can fill in an userid.
Upon clicking the button I want to open a popup (url) on the browser of the specific user with the session userid equals to the userid filled in the textbox. The popup should appear immediately an not require a page request to get shown.
What kind of things I should use for this?
I googled, but couldn't find anything usefull.
Writing a chat client can be quite tricky because you have to send data from the server to the client when a message is sent by another user. WebSockets allows you to do this but it is a pretty new technology which does not work with IE versions below 10. Chrome, Firefox and Safari do support it.
The group of technologies that allow you to use javascript to communicate with the server instead of requiring browser page refreshes is called AJAX.
A library like DWR makes it very easy to do AJAX between Javascript and Java. It also has a feature called Reverse AJAX that allows you to write Java code that executes javascript code on the client. http://directwebremoting.org/dwr/documentation/reverse-ajax/index.html
With DWR you can write this to show a popup on all connected clients:
Container container = ServerContextFactory.get().getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
System.out.println("sessions: " + manager.getAllScriptSessions().size());
for (ScriptSession scriptSession : manager.getAllScriptSessions()) {
System.out.println("Sending script to session " + scriptSession);
ScriptBuffer script = new ScriptBuffer("window.alert('hello from reverse ajax server');");
scriptSession.addScript(script);
}
When you need the popup to appear immediately and you can't wait for a page-request, you need some kind of communication channel from server to client which allows pushing messages.
A good tool for this are Javascript WebSockets. A WebSocket is a bidirectional connection between browser and server which is open while the page is open and which can be used by both sides to send data whenever they want.
On every page you need to create a websocket and connect it to a WebSocket Servlet. While the client has the page open, that servlet is able to send a message to the client whenever it wants. You can then handle that message in Javascript to implement a real-time chat application.
Related
I have a REST API made with Spring Boot. I can add entities (Persons) to a database using HTTP requests.
I would like to have a web page display those entities dynamically: whenever a new Person is stored in the database, the page displays it on top of the list. No need to refresh the page.
What are the ways to achieve this? Would JSP be enough? Do I need javascript?
The simpler the better.
What are the ways to achieve this? Would JSP be enough? Do I need
javascript?
Yes you need javascript. But you can embed it in JSP too.
This is a solved problem, you will find numerous examples in the web if you search for "websocket spring". Example https://spring.io/guides/gs/messaging-stomp-websocket/
If you are not stuck in JVM-land and RDBMS, I would encourage you to take a look at firebase-angular stack (3-way binding)
Further read
Read this https://stackoverflow.com/a/12855533/6785908
Shameless copy since posting just a link is not advisable as link may break in future
In the examples below the client is the browser and the server is the webserver hosting the website.
Before you can understand these technologies, you have to understand
classic HTTP web traffic first.
Regular HTTP:
A client requests a webpage from a server.
The server calculates the response
The server sends the response to the client.
Ajax Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at
regular intervals (e.g. 0.5 seconds).
The server calculates each response and sends it back, just like normal HTTP traffic.
Ajax Long-Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
The server does not immediately respond with the requested information but waits until there's new information available.
When there's new information available, the server responds with the new information.
The client receives the new information and immediately sends another request to the server, re-starting the process.
HTML5 Server Sent Events (SSE) / EventSource:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
The server sends an event to the client when there's new information available.
Real-time traffic from server to client, mostly that's what you'll need
You'll want to use a server that has an event loop
Not possible to connect with a server from another domain
If you want to read more, I found these very useful: (article), (article), (article), (tutorial).
HTML5 Websockets:
A client requests a webpage from a server using regular http (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
The server and the client can now send each other messages when new data (on either side) is available.
Real-time traffic from the server to the client and from the client to the server
You'll want to use a server that has an event loop
With WebSockets it is possible to connect with a server from another domain.
It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to
implement the client side, which is very easy!
If you want to read more, I found these very useful: (article), (article) (tutorial).
Comet:
Comet is a collection of techniques prior to HTML5 which use streaming
and long-polling to achieve real time applications. Read more on
wikipedia or this article.
I am trying to login to web outlook of test exchange server. Using selenium webdriver I am able to input the username , password and then click on the sign in button in web outlook. But after clicking there is no response in selenium neither throwing exception.No actions beyond the click will be performed.
But same scenario if I am running with HTTP instead of HTTPS, everything works fine.
Does it has to do something with IE settings? I have unchecked 'Warn about certificate address mismatch *" in Advanced section of Internet Options. This is done to bypass the SSL certificate error.
There may be few reasons.
1)Element may be out of focus. try focusing the element by using sendKeys(Keys.CONTROL).
2)There may be a page refresh happening or AJAX call when you are trying to click element. try using implicit wait until ajax call completes.
3)Website which you are trying to automate may be Siebel application(containing Active X Control)
I am using Atmosphere RC 2.4.5(java 1.8, jsp, tomcat 8, and javascript Atmosphere client) for pushing messages from server to the clients. Everything works as expected, and didn't have any trouble with the implementation of it, but now I have doubt about the next thing:
I implemented a feature where admin user can send some notification to all users currently using the app. But if in that exact time while the message was pushing through web sockets, some user clicked link and started navigation to another page. His web socket would be closed and he would never get the message.
Does anyone know how this can be achieved using the atmosphere, so no messages would be lost.
Thanks all.
You can find your solution based on this page from documentation of atmosphere-js :
Sharing connection between Browser's windows and tabs
By default, the atmosphere.js library will open a new connection (based on the available transports: websocket, long-polling, streaming, sse, jsonp or ajax) every time a new window or tab is opened by the Browser. In some case it may be more efficient to share a connection between windows/tabs. To enable the mechanism, all you need to do is to set the shared property of an AtmosphereRequest object:
Here's example :
var request = new $.atmopshere.AtmosphereRequest();
request.shared = true;
request.transport = 'websocket'
$.atmosphere.subscribe(request);
One of these lines can be the solution of your problem :
request.shared = true;
I am looking to send a facebook message from my application server without
the need to prompt the user for input each time.
I have been looking over the facebook chat API but it only works with facebook UI
which means that the user must be prompted every time I want to use the chat.
Is there a workaround or am I missing something?
I am using JAVA on my server.
I have two users in my machine and I'm running a code that opens a ServerSocket on a specific port via an ajax call. What I'm trying to do is detect which user has opened the Socket.
For example: I'm current logged in with user "admin".
So, I make an ajax call and it opens the ServerSocket. If I run System.getProperty("user.name") it returns "admin" as expected.
But, when I change user to "lucas" and try to make an ajax call to the server, when I run System.getProperty("user.name") it stills return "admin" probably because the code is running on that user.
Knowing this, is there any way I could tell my server which user is making the ajax call?
Briefly, no. A socket has no information about who has opened it. You could track the host that has opened it, and that may be of use if you have a host/user mapping, but what I really think you want is some form of authentication for your clients. In that scenario, your user will open a socket and be challenged for identifying info. That will allow you to determine the user from the server's perspective.