This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Java Standalone Program to JSP communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java Standalone Program to JSP communication" Watch "Java Standalone Program to JSP communication" New topic

Java Standalone Program to JSP communication

aparna bhogu

Joined: Aug 03, 2014
Posts: 7
The following programs exist: 1. I have a java application which accepts bio potential data every second or two and stores it in the database. This is a socket server which accepts this data from multiple clients and spawns a new thread for processing it to store in the db. 2. I have a jsp page on tomcat server which reads historic client data from database (stored by application 1) and displays it on the page.

The socket server program in 1.) above is not running inside of tomcat server.

The new requirement now is : Display all of the human data coming in live on the jsp page.

Now the problem: I will now need to pass the live data from socket server (which is stand alone) to the jsp which is running on a tomcat server.

Possible solutions:

APPROACH 1: Run the socket server in the tomcat instead of stand alone and store the frequently incoming data in a java object so the jsp can access this object every second and display it on a graph.

PROBLEM : The stand alone java application does not need to be included in a tomcat server except for the fact that the jsp needs access to the live data. Also, I have read that this is not the best way.

APPROACH 2: Expose the stand alone java application as a web service and communicate with the jsp using REST architecture.

PROBLEM : The complication of using this method is that it will not have the flexibility offered by websockets or server sent events (SSE) of auto updating the latest data. The jsp will have to keep polling for new data every one second which is also not a very good option.

I need suggestions on which is a better method for accomplishing my task. Or is there a third better way which I have completely missed.
K. Tsang

Joined: Sep 13, 2007
Posts: 3134

Welcome to the Ranch.

Interesting problem you got.

For approach 1, when you say "run socket server in tomcat instead of stand-alone", do you mean inside the web app? If this was what you meant, then no, it is not practical.

About fetching data, doesn't the JSP always have the latest data from the database (eg up to the last record in the table at the time of the query)? Does the socket server and web app use the same database?

Lastly, is it realistic to update the output page every second? The JSP output I presume is user triggered, if such page is updated (refreshed) every second, the page would be flickering all the time!

I can suggest: Run the stand-alone socket server OUTSIDE tomcat container in the same (physical) server. Have the JSP page access the database used by socket server to get the data. (this would just be a configuration issue)

Hope this helps.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I concur with not integrating the socket server with the web app. In fact, they should be entirely decoupled (so no web service either) - data gathering and data display are unrelated issues. You want either activity to continue even if the other is shut down for some reason.

Display of live data calls for some AJAXy thing on the web page - I am certain there are jQuery plugins that not only display live data, but can also be configured to update it every so often. That also avoids what K. Tsang refers to as "flickering", because the page would not reload.

You need to specify what "live" means, though - obviously, real-time is not possible, there will always be a delay. So the question is how big a delay is acceptable - a maximum delay of 2 seconds might need other approaches than one of 60 seconds.
aparna bhogu

Joined: Aug 03, 2014
Posts: 7
Thank you K. Tsang and Ulf Dittmer. As you both rightly pointed out, I can make the JSP read the database directly rather than using the more complicated approaches I was thinking of. Thank you so much for pointing me in the right direction. Your responses have been most helpful.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

Except, of course, that database reading should not be done in the JSPs. Be sure to structure your web app correctly.

[Asking smart questions] [About Bear] [Books by Bear]
aparna bhogu

Joined: Aug 03, 2014
Posts: 7
Thank you for pointing that out Bear Bibeault. Will keep that in mind.
I agree. Here's the link:
subject: Java Standalone Program to JSP communication
It's not a secret anymore!