Java Standalone Program to JSP communication - java

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.

I have a java application which accepts bio potential data every
second or two and stores it in the database
You already have the answer: just display required data from this database in your jsp page. This will be easiest solution.
I undestand that you're trying to display realtime data, but JSP itself is not designed for realtime output, you will have the delay anyway and because you already have required data in database - no need to transport it to Tomcat server.

Related

What is the most secure way to add an account login + HWID lock to a Java program?

So I have a Java Program that just runs in the windows console at the moment. When the program first starts, I want to have it go through a login prompt with a username & password. I’ve previously used a MySQL database to check if a username + password combo exists, and then allow that user to login. I know how to encrypt passwords and such when I compare them to the database info. However, when I did this, the MySQL login details were left in the code so that the program could connect to the database. This leaves a huge problem in that someone could decompile the program and find those login details, then use them to access the database.
So my question is, is there a way to access these logins using the program, WITHOUT exposing my database details to a snooping person. Is there a library out there that could accomplish this?
Yeah you can do it, But you need to re-architect your application.
You should separate you application into two different application.(In short: you have long way to go)
1- Client Application: Where your Java Application is interacting with the end user.
2- Server Application: Where your Application is interacting with your DataBase. This Application can talk to your Client through your network.
Note: keep in mind that, any application at client, can be decompiled. All traffic even any communication between client-server can be monitored.
There are many methods out there, to make the communication between the server and clients. The choice is yours, you can make your own one (Socket communication) or follow some standards like REST or SOAP.
If you want to use REST or SOAP, there are many ready framework and libraries available where Spring is one of them. Since you need to have Server Application(Ex: Rest Server), you need to run your server application in a container, where in this case, Tomcat is the most famous one in JAVA world.
But there is better way to Start ; you can make an Spring Boot Application for your Server app. Most of the libraries even your container is already embedded, and you only need to focus in your Application Logic. Once it's done, you just need to run a single jar file and your client can start talk to the server app, through network. The drawback is that you need to follow the spring boot standard.
You can also Secure your Rest API by Spring Security framework (Which is totally separate topic), but you can simplify it to, token exchange between your server-client for each REST call.
So in this scenario, let say someone decompile your client application, he would
see nothing but, some rest API links, which can not work without login and token.
Also keep in mid that you also need to Secure the communication between Server and Client Throw the Network by TLS.(Which is totally separate topic)
If you're giving out db access then deal with the side effects of that choice. I think mysql offers some kind of row level security option. Check the docs.
If you want some level of backend obfuscation then put it between your users and backend via REST or some other scheme.

java program client side to trigger a server side java program

i'm planning on creating a system that has 2 parts, the first part will be found in the client side while the other part is in the server side. there are 2 different programs here running, the client side will save a data in the database and raise a flag or a trigger which will then be the cause for the server side part to start and retrieve data from the database and process it in an API.
at first i decided on using a scheduler on the server side to continuously run the program and check for any changes in the database using a "status" field, but then this is not that efficient as it would be wasteful to always check for changes.
thus i was hoping you can help me out in finding a different solution to my problem here so that i may do this more efficiently
You can use database triggers, upon a change in your db you can call a server side method embedded in a jar file.
The following link might help.
https://dba.stackexchange.com/questions/39522/execution-of-a-java-program-by-a-trigger
Update :
You can use asynchronous messaging services like jms which would work on publish & publish service, you would need a broker server for this is this way more efficient & elegant :)
For start you can follow to the guide from spring: "React.js and Spring Data REST"
https://spring.io/guides/tutorials/react-and-spring-data-rest/
Here Spring Data Rest on Back-end side with events(4th part of guide, right what you need 'from the box') and ReactJs as Front-end client.
If you are new in ReactJs it won't be a problem, you can follow guide from scratch.

How to access one web application from another web application (jsp)

I have two web applications in jsp:
1st application is running on tomcat 6
2nd is running on tomcat 7 which resides on different machine
now I want to call the jsp of 2nd web application from 1st web application and also want to pass some data during run-time.
So how can I achieve that, please suggest me some solution.
It was very simple both will be running on some URL alone so redirect that to other. The major thing you should concentrate on the passing data should accomplish a same data store i.e., both should point a same database or any other data storing technique. You have to design a database such that both should use the same data without any conflict.
If you want to use without any central repo you can pass the data by building it on an XML,JSON or any other technique that can act as a data carrier between two applications.
You can't directly access JSPs on a different server for various reasons (security among one of them).
What you can do:
Use an iframe to display a remote URL inline
Use a HTTP client library on one server to access the second server via HTTP
Add a JSON servlet to the second server which gives you access to the data you need. This allows you to use JSONP to access the data directly from the client or to process it with a JSON framework on the first server.

How do I update data to all application java of shared database

I am creating a web application. Same application will run from different machine, but
they will use common database. When application starts it will get all data from database.
But when I update data from one application, how other running application will know that data
has been updated, I want when I update database form any application, other running
application will get notification immediately, and they should update their data.
One possible solution is I can take all applications URL in a list, then after updating value
I will send request to all application, but how to do this using send Redirect. Is it correct way ? or is their any other easiest way to do this. Please help me.
Is it a requirement that data is loaded once on startup? If it's not than you can just read directly from the database with a low cache invalidation time.
In case your apps need to be synchronized "almost immediately" I would do it like this:
You can set up a messaging server which would create a JMS topic. All of the clients will listen to messages from that topic. When one of the apps update something in the DB it will send a message to the topic. The rest of the apps will get the message and update.

File upload by mulitiple user

I am Uploading a file into a webserver using File upload API it works good for a single user, if multiple user upload a file simultaneously how to improve my code using threads?
What type of web server are you using? Typically web servers process separate requests on separate threads, so you shouldn't have to do anything special, your web service code will be inherently multi-threaded.
According to the Servlet API each request is supposed to be processed by a single thread, therefore you shouldn't have any issues.
However if you're trying to maximize the number of users your server could potentially service then you might take a look at advanced connectors for Tomcat or whatever container you are using
http://tomcat.apache.org/tomcat-6.0-doc/aio.html

Categories

Resources