Using Java Program as part of Java Web APP - java

I have built a java program that does very simple reading, storing, and processing of server side data that it receives from some electronic sensors. This information now needs to be put onto a webpage.
Some calculations need to be done server side so I want to keep the Processing done in java rather than an interpretative language like javascript or python.
Anyway How do I get the information from the java program up onto a website using a java web application? I am a little unfamiliar with JSP and Servlets though they aren't too complicated I do not know how to get my current working program to work with those and print out the server side information.
It doesn't need to be anything fancy this is just a simple program to do some real time remote monitoring over the web of some electronics.
I do know and understand HTML as well so i do know how to work with those.

You can use tomcat+spring, here is link to a tutorial that teaches you how to create a RESTful webservice https://spring.io/guides/gs/rest-service/
What you'll need to do is basically mapping the http requests to some functions that will call your functions in your original program.

Related

Is it possible to use more than one framework at the backend(Spring boot + Django)?

tl;dr: Is Spring + Django back-end possible?
When I was new to industry and was still working my way around the office, I got interested in Django and created a very small, basic-level application using the framework. When I got to meet my team after a few weeks, they said to go for Spring framework. After spending half a year on the framework and the main proj, I finally started to get time to start working off-hours. But, I don't want to lose both the skills - My teammate(when we were still in office ;) ) once told me that they worked on a project that started with python code, and then later added features using Java. And I am unable to find any helpful google searches(mostly showing Spring vs Django).
How should I go about it? Is it too much to ask for? Is it worthwhile? Will I learn some new concepts of application architecture a noob like me would have missed. Please provide me with some insight.
Are there resources(docs) I can go through?
P.S. I'm not a diehard fan of either of the frameworks right now, just another coder testing waters.
You can't write java in python.
You can extend Python with C/C++ which is quite common: Extending Python with C or C++
And about the part that they told that they added features with java:
It's common to create different parts of a project using different languages and tools. Microservice architecture is a common architecture for these kinds of use cases. You basically code different parts of the project in a language you want and then you connect all the parts using different methods like REST APIs, gRPC and etc.
Imagine you are creating a website like youtube that lets others upload videos. There is a form that users upload their files and you store them in your storage and then you have to encode the video file for different qualities. You can code the form handler using Python and Django to store the files in your storage. Then you can code another service using java that handles the encoding part which is a heavy process. When an upload is completed, you send the file or file path to your java service using an internal REST API and tell the service to start encoding the video and notify the Django service and then the Django service will publish the video on the feed that can itself be written in another language.
I would say go for 1 framework and stick with it. For example Django if you want to code in python, and spring if you want to code in java. Learning both frameworks however brings a lot of value, because you can compare their benefits (eg. spring forces you to write clean code, django has build-in and simpler database management)
I like Django's build-in tooling a lot, you only need to know python for it to work. Spring requires a bit more knowledge of eg. hibernate for database management. However I predict Django will outgrow spring at some point, because of cloud valuing fast iteration over code and quick startup time (auto-scaling apps) over large overhead apps and long boot times. Hoever, if you like java, I can recommend JHipster for java/spring webapp development to get up to speed very fast and learning the ways of REST CRUD api fast.
To combine 2 programs: write your main logic in one app, and write a small service in the second language, making sure its independent of the first app (no back and forth communication and complicated logic, but simple independent request/response, as if the main app was never there). Add a REST api to the second app and use eg. http requests to communicate.
What's possible in terms of combining languages:
connect different applications with each other: by letting them communicate through their APIs. For example a python api developed with flask or django can send requests to a java api developed with spring, as long as they have a way to communicate (eg over http, or via some queue like rabbitmq)
connect a webapp to 2 different backends: by using a shared authentication system: For example a keycloak authentication server to handle tokens, that your backend applications know about.
What's not possible (and also not preferable):
combining java with python code in the same program: there are some hacky ways to get it to work, but its asking for trouble and not readable.

how to use java code from .net desktop application?

I want to Use Some Java Code From the WCF Service. Basically I need to intreact with the java application from the .net so how can I make it out for that? Any guidance for that?
I was just wondering that how can i call java code from my .net desktop application? Do I need to create .exe file of my java poject or jar? How would I invoke that java project from my .net desktop application?
Like some code in .net desktopp application will call
void startprocessing ();
code written in java application
Let me Clear the Things What i want to implement..
I have one desktop appplication of windows and I want to integrate my speech recgnition facility in that application which is in java. What I want is to exchange the audio data as binary and some strings from .net application to my java application so I can process on that binary data to convert wav file and etc and recginze it..that is just completed ..but what I need to know is how to send those strings and some binary data to java application and just invoke that java application ?? Do I need to 'use'(?) wcf or other service ?
Going out on a limb here, sounds like you java app needs to expose the method you want to call via a web-service. That's of course assuming you have control over that in terms of the java app you're interacting with. Not sure though you can access that method simply by compiling your java app to native.
You can take a look at JNBridge, which provides interop possibilities between Java and .NET. However, as another poster has suggested, exposing the functionality through a web service is probably a better solution.
A quick Google search will give you plenty of information on how to create web services in Java, but for starters you can take a look at: http://docs.oracle.com/javaee/6/tutorial/doc/bnayl.html
I have Successfully done that with the axis 2 webservice with tomcat apache server with the following link..its too easy .i really got succeeded
http://blog.sencide.com/2011/06/create-web-service-using-apache-axis2.html

Java & Asp.NET cross data

Someone here is developing an app in asp.net (Visual Basic) as this is their preference language.
Now the method he's creating (as he already started work on it) will carry out multiple databases queries.
I have a java application i.e. Java EE that requires the same output from the function of the vb application.
Is there any way I could exchange the data as in send data from jsf to asp.net, process in vb and get output back in jsf?
The only data I require is a string consisting of 8 characters at the most. So not serious data to be transferred.
The asp.net application can expose some web services to your Java application to call and get data from.
This is very easy to accomplish with asp.net and should be standard enough for your Java application to call and retrieve the results.
try using sockets as that is much easier to achieve. if you are brave enough use jni which us lot more hard work.

is it possible to have a hybrid PHP+Java web application on Google App Engine?

I want to create an application that submits same/similar data to sites containing web forms. These sites use PHP scripts...
I have a php script with me, that submits data in the manner that i require, to such forms. What I want to do is, design an entire web app around this code... I tried to obtain equivalent java code that does what the php code is doing, but could not obtain such code...
Since Google App Engine supports Quercus framework for PHP, what I am thinking now is, use the PHP code for actual submission of forms to their actions, and rest of application (that tracks all submissions and does other stuff like login/logout...) is in Java.
This would require some method by which I can pass the relevant form parameters from Java code to the PHP script, then some way for the php script to return the response of each submission back to the java code.
Is such an application doable? Pls keep in mind that I want to use Google App Engine for this purpose.
Nowadays it is possible to use different languages in different modules of the same app.
See Using both Java and Python with the new "Module" feature on AppEngine?
It should be doable with Quercus. We deploy a Python app that has some Java/Clojure backend processors but both languages are "native" to GAE. Using PHP will be a serious pain and not worth it all. I suggest that you just learn Python you will master it faster than using Quercus on GAE.
If you also go after PHP you stick to your LAMP knowledge. I suggest thay you do now!
Disclaimer: I wanted to use JRuby on AppEngine but I had a lot of difficulties (plus you have to know how everything works optimally on GAE) so I took up Python

Send JSON data from JavaScript to a Java program on the server

I have a webpage that does a lot of processing in JavaScript and creates some nice tables/graphs/etc. I want to export this to Excel and have not found any PHP libraries that can handle charts, so I wrote a Java program using using Apachi's POI library. Now the problem is, how can I send the JSON data to the Java program. Initially, I thought I could just send the JSON data in a POST request to a PHP page, then have PHP do a system call and pass the data on the command line. But 700,000 characters might be a bit too much for the command line. I looked into JSP and Servlets but have no experience with those (note my server doesn't have this installed either). Any suggestions on how to send data directly to the Java program?
Update: I found out you can invoke method calls in a Java Applet from JavaScript. I think this will be the easiest way to use my existing Java program with minor modifications.
You don't need to have JSP, one way of achieving this is writing a HTTP Servlet, drop it into a servlet container such as Tomcat, and have your PHP do a POST to the Servlet.
This opens a whole Java EE paradigm. ( :
Otherwise, you may check if PHP allows you to run command line batchs, which runs your Java program, then in your Java program output the results of the call to a text file or database, then have your PHP program read it.
No, 700k (600kb?) characters is not too much for a "command line", i have seen batch jobs running on way more megabytes of data.
Although I'd favour the solution with a Servlet there is a relatively easy way to communicate streaming data between different programmming languages. In your case, you could open a TCP socket in Java in listening mode and connect to it from PHP and then forward the data from PHP to Java. There is socket support built-in in PHP (cf. socket_write) as well as in Java (Socket). Have a look at the official tutorial for an introduction in Java socket programming.
DWR can be used to send JSON object to java program .
Ajax for Java developers: Ajax with Direct Web Remoting

Categories