Accept commands in Java application, send from ASP.net web application - java

I want to achieve some kind of remote control web application.
What I have now is a java application that runs on my computer. I want it to listen for commands from a web page, which I run in ASP.net on my own server. The Java application should do it's business, but at the same time listen for commands. If a command is recieved from the server, it should break what it is doing, and do what the command says.
Example:
*The Java application is currently feeding dogs. It gives them 1 treat every second.
*I navigate to my web application, and press the button "Feed cats 1 treat every second".
*The Java application stops feeding the dogs, and starts feeding the cats.
*I navigate to my web application again, and change the amount to 4 treats every second.
*The Java application changes the amount to 4 treats per second.
How would I approach the connection between the Java application and my ASP.net application? I've read about Sockets and Serversockets, but I'm a little unsure on what to use for this exact issue.

Related

Server side program [duplicate]

This question already has answers here:
How to run a background task in a servlet based web application?
(5 answers)
Closed 1 year ago.
Sorry if asking sth dumb but I'm completly beginner. I'm trying to write simple web app using java webapp archetype (servlets/maven). In app clients will be sending some data to server, then that data will be precessed on server side and send back to clients after constant interval. My problem is that I don't know how to create method that runs continously on server side (something like main class). Is it even possible to create sth like that?
At a low level, this is what a server does: it listens on a port for client requests and when a request arrives, the server processes it. But if you want to build a web application, you don't have to work at this level, but at a much higher level: you run your code in a web server like Tomcat or Netty, that handles all the low level work, and just invokes your code when a client connects to it. So I would advise you to first choose a server and follow a tutorial on how to run a web app, for example read the Netty docs.
If you want to have some background threads that do work independently of the client requests, you can register a ServletContextListener implementation, that let you do work when the context is initialized (your application starts) and destroyed (your application ends). For example see the tutorial at https://mkyong.com/servlet/what-is-listener-servletcontextlistener-example/

View running application output in the browser, how?

I have a simple cloud IDE,I want to make it able to build and run applications remotely, the target application's source files will be in a remote server in isolated virtual machine (e.g Windows 8.1,or Ubuntu 14.04). It's not difficult to build that application but how to run it and view its output to users ?
What if it's a desktop application (suppose it's written in C# or Java or Python)?
Note: users access there applications only using browsers (e.g Firefox,Chrome,...)
Edit: desktop application may contains GUI stuff not only console ;)
You need a web application.Now this web application when loads send request to backend code that backend code will do SSH to remote machine and read the file from specific location.Now that read stream will be send back in response and displayed on web based UI. In these type of application few thinks matters.
1) Like if you whole file at once then it will take time to display that content to user.Better idea will be read around 100 lines at once and when user scroll down then again send request to web server to read next 100 lines in this way you can decrease response time and better user experience.
Each of the languages you mentioned offers a Web Services framework of some kind. Pick one, and implement something that a) starts your app, b) shows the output. Depending on the processing time (how long it takes to complete) you might even get away with just one.
You can go for a self-contained, standalone service:
C#: Is it possible to create a standalone, C# web service deployed as an EXE or Windows service?
Python:
Best way to create a simple python web service
Java:
https://technology.amis.nl/2009/06/05/publish-a-webservice-from-a-poja-plain-old-java-application-that-is-out-of-the-container-using-endpoint-class/
Alternatively, you might use a container (server) for your app, like Apache with mod_mono or IIS for C#, Tomcat, Jetty, Jboss for Java, Apache with mod_wsgi for Python (just examples).
The web service would probably sit on the remote machine, so it could use system calls ('command line') to run your core app, and then it would send the results over http. Since you mention GUI, there could be more layers to the solution:
The GUI - static HTML, desktop app, sending requests to the 2nd layer, say displaying dropdowns for parameter1 and -2
The Web Service - takes the params from the request, say http://remote.machine.land/start/app?parameter1=X&parameter2=Y, runs a local command like /home/users/myapp.sh -parameter1=X -parameter2=Y
The application itself - not necessarily aware of any internet out there.
This way you stay free to change/enhance any part at a time, call the 2. layer programmatically, introduce load-balancing, etc.
3.

Confusion upon the difference between Java Client and Java Server

From my understanding the Java Client is what is rendered onto the browser such as the javascript, "the client side" for example what the user see's (tables, html, the sorting etc) things in the jsp. The server side is the servlet that makes the connection and call the specific class's based upon the request. I feel as though I am completely wrong as when I do research I am getting different answers. CAn I get some clarification on this please.
Java running on the client side means the end user needs Java installed and is running a Java program on their computer. This could be a standalone Java executable program (a traditional application), or it could be through a browser running a Java applet. Either way the end user's computer is actually running the Java code locally.
Java on the server side means that all the Java processing happens on a server somewhere, and the person accessing the site or service doesn't need Java installed. Servlets and JSPs fall into this category. The server does all the processing and then returns HTML/Javascript/CSS, etc to the end user. The user's web browser receives and processes those files to display the web site.
It sounds like you are getting confused because web browsers use Javascript to display dynamic sites, but Javascript is a totally separate language that has nothing to do with Java. In other words, a web site could be powered by Java servlets on the back end and could also use Javascript on the front-end, but they aren't directly related or dependent on each other. All the HTML, Javascript, CSS, etc that make up the web site are processed by the user's browser and the user doesn't have to know or care how all that stuff is generated on the back end.

Printing in Java Web Applications

I developed a stand alone application for a medical channelling centre with VB6 seven years ago. There is a mandatory requirement of printing of a chit with a small size(219mm to 140mm) with a single click without the print dialog. Intermittently they need to take reports to full A4 pages from a different printer.
This could be easily achieved in VB6 with setting the printed and paper properties in variables. Now the center want to convert it into a web application, but beforehand they want to make sure that the printing capabilities can be achieved in such web application. They want specifically not to bring the printer dialog box every time.
Is it possible to change printer and paper properties in a web application with Java EE without bringing the printer dialog?. (at least with the support of Jasper reports, etc)
It is possible; however, you need to keep in mind which application would be printing.
If it is the web browser that is printing the web page, you are out-of-luck, as the web browser is already coded and will do exactly what it will always do.
If it is an application embedded within the web page, provided the application can connect to the client machine's printing resources, it is possible; however, often such a task implies that the application is trusted as a remote machine (the web server) is now using local resources (client disk and printing configurations). You may need to pre-configure the machine to elevate that application's level of trust, or you might get a popup declaring that a remote machine (the web server) is trying to use local resources.
If it is a multi-tiered application, and the web server receives the request to print from the web application, then the web server will be doing the printing. The web server trusts itself; however, its environment might be quite different. It may or may not have access to connect to nearby printers, depending on the rest of the network architecture and security policies.

Hardware support from a web application

I have a web application running with support for some specific pieces of hardware. This is achieved in the following steps:
User runs a small installer that places java files (and a couple
others) on the client machine. The main piece is a jar called "hardwareManager"
User visits web app. The web app runs a java applet which, due to
a .java.policy file placed during the install, has permission to
interact with the client machine outside the browser sandbox.
The applet checks to make sure the hardwareManager is running,
and if not runs a command to start it.
User interacts with the web app which sends commands to the applet via
javascript. The applet then writes commands to a text file
on the client machine. The text file is constantly monitored by the
hardwareManager which runs any commands it reads in.
This works, but seems clunky. I have a couple ideas on how to improve it, but I don't know which, if any, are even worth trying.
Would it be better to set up the hardwareManager as a socketServer and have the applet connect directly to it, rather than going through text files? Is that even possible?
Is there a way to eliminate the applet altogether and have the javascript talk directly to the hardwareManager? Maybe by writing the hardwareManager to be a local http server? What port should it run on? Do javascript xss limitations fit in here somewhere?
It would be less clunky to start the Java application using Java Web Start. This would remove the need to daemonize or install the Java hardware manager.
Another alternative is to use a built-in browser inside Java. I supose this is not an option, since you depend heavily on Javascript (I suppose to provide a rich client experience).
If you already have to install something on the client machine, why did you make the choice to go with a web application?
Talking from experience: We had a Java EE application which needed to print to PoS printers at the client site. We installed a small "synchronizer" application that connects through SSH and synchronizes all clients files. Afterwards, it loads the JAR and executes the program. This program connects through RMI with the server and subscribes to a JMS queue to receive the print assignments.
Applied to your case: Why not let your Java application connect to the server directly? You can use HTTP, SOAP or even JMS over RMI. You can then launch the hardware command from the server (instead of from the limited JavaScript webbrowser environment). This way, you get tons of features: authentication, buffering of commands, and you can even share hardware between multiple clients.
Schematic:
<----AJAX------> Web browser
ApplicationServer
<---HTTP/SOAP--> Java hardware manager application
You can launch the Java application using Java Web Start, which allows you to update the application automatically (instead of needing to pass every client a new installer).

Categories