How can I use a browser as gui for java? - java

I want something similar with php/jsp in java, but not on the server side, but on the client side.
Maybe I can make a html file with i/o, or with some net.* classes. But what can I use for the button on the html page ?
Thank you

It sounds like you want something like the Google Web Toolkit. You write your UI and logic in Java and it gets compiled into Javascript that then runs client-side in the browser.

Perhaps you mean you want to run an application on the local machine, but use an HTTP-based interface. In that case, you need the application to act as a web server on the local machine, then launch a web browser for "http://localhost:8080/" (or whatever port the application is listening on).
To implement this, you need an embedded web server such as Jetty.

install resin on your PC
http://caucho.com/resin-4.0/admin/starting-resin.xtp
(and JDK)
can run jsp,html on http://localhost

The question is a bit vague... maybe something like Rhino or MozSwing will help??

Related

Can a Java Applicaton and a JavaScript running in a browser communicate with each other?

I have a JavaScript embedded in a website, running in a normal browser, and I have a Java Application running on the same machine. I am looking for a way have both communicate with each other.
I assume that creating files in the local file system from inside JavaScript running in a browser is out of question.
The only way I came up with would be to use a server that both programs can send messages to, and from which they poll for new messages.
Is there any other other way to accomplish this?
A couple of ways I saw in practice:
You Java Application may listen to some local port which your JS will access for instance via XHR. You'll need to mind cross-site scripting (your JS may need to be loaded from that local URL), but this is doable. The easiest would probably be to run an embedded HTTP server.
Your Java Application may be registered as a protocoll handler in the OS. Then JS would open links registered with the application thus sending data to it.
As #PavelHoral is pointing out, CORS is a way to workaround same-origin policy.
JavaScript inside a browser can only make AJAX requests or communicate with browser plugins if they provide some additional JS interface.
Local HTTP connection
One option is to listen for HTTP connections in your Java application (does not have to be servlet).
You will need to handle CORS correctly.
Central server
Other option is to have a central server to which both your JS and Java code will connect.
Java applet
Another option is to have Java applet. When running in privileged mode you can do pretty much anything (maybe you can convert your Java application to Java applet).
You will need to handle applet security here (e.g. signing applet with trusted certificate).
Please refer to this question for direct sockets communications from within javascript. You'll need a HTML5 browser, and likely this won't "just" work or necessarily be a good idea if you want to publish this on the WWW.
How to Use Sockets in JavaScript\HTML?

Java program to a web service\servlet

I've written a java program that handles certain http requests.
Now I'm asked to run this as a web service in IIS.
I've tried to export it as a jar and maybe run it like that...but I didn't succeed.
As far as I understood, I need to re-write the program as a web service or servlet and upload it this way to IIS.
My question is, how to do it (I'm using eclipse)? So far I managed to run a server but I never couldn't make my program to work. Can I have guidance please?
This is pretty old article but you can take a look here
For new IIS I'm not sure you can because Microsoft has it's own tech called .NET so I suppose it's better to use other server or use .NET
Also you can use Tomcat connector for IIS which is filter with redirect

Standalone Java application with HTML front end

I want to develop a standalone java application, with web browser as front end. This application will run locally and won't be making any remote server calls. I'm essentially using java, as web-browser cannot perform file operations.
I want this application to be portable: no need of installation. Just copying a folder should be enough. I want to know how it can be done, how will javascript communicate with java code.
In continuation of #Quentin's answer.
Yes, you need web server.
There are 2 principal architectures:
Create stand alone application with embedded web server
Create ordinary web application and run it on proprietary web server.
IMHO I think that the second approach is better, however it strongly depend on your application functionality.
You can take jetty or grizzly as a web container. Both can run as in embedded or stand alone modes. You are welcome to share other details of your application with the community if you need concrete advises concerning to the design of your application.
The application would need to implement an HTTP server. Then all communication would be done over HTTP.
Write a small web application as you need and Deploy it using Jetty. Jetty is a pure Java-based HTTP server and Java Servlet container. You can use it by embedded mode also.
Deployment is so easy if you use Jetty-Runner
java -jar jetty-runner.jar my.war
You don't need a local web server. Take JavaFX (embedded webkit) and implement a URL protocol handler for say "myprotocol". Then you can access it from the browser using something myprotocol://xxx.yyy.zzz

Android host website on phone

What approach can I take to solve my problem such that my Android app hosts a website (on whatever port > 1024) and also features a way to allow/block incoming client. So far i have tried manually opening a ServerSocket and injecting html then closing the socket, however it only works most of the time and only with Google Chrome...anyone have other ideas?
You could use some small servlet container like jetty - pure java and lightweight enough to fit a phone. Then you can register your servlets and maybe deploy complete war archive
Checkout iJetty. They basically ported a fully-functional Jetty implementation to Android. I've used it before both as a servlet container (deploying WAR files to it) and as an embedded server in an application I wrote. Getting it to work inside of another application takes some doing, but can definitely be done. You also have access to the source code so you can modify as needed.

Creating a simple web page using Java without using Servlet and Tomcat

I want to create a simple server application which runs on the desktop, and when I type my ip and port on the web browser, it connects to the server client which then opens a webpage with appropriate displays coded on the server application.
I read online that I need to use servlet and Apache Tomcat to make a webpage using Java.
I am wondering if there are any easier way to make a simple webpage which can contain buttons without using servlet and Apache Tomcat?
For example, I can use sockets to communicate between server and client applications. Could I change this client into typing the ip address and port on the web browser which will display a webpage created and contained in the server application and remove the need for servlet and tomcat? If so, how do I create a button on the server application so that web browser can see the button when connecting to the server application?
Thank you very much.
If I understand you correctly, you want to have a web page, but you don't want to use Tomcat (or any other servlet engine).
Although it is technically possible to write your own little web server (using server sockets etc), but what you're basically doing then is rewriting Tomcat. Writing a good web server is a daunting job, and should not be taken lightly. I think you are underestimating that. Instead, use what is already there. Tomcat is really quite easy get running.
Creating the server piece could be done with raw sockets, but I would look at at an embedded server like Jetty. I think it will save you a lot of time and headache.
http://docs.codehaus.org/display/JETTY/Embedding+Jetty
You should look into Play Framework. It will be easier then implementing Tomcat or Apache.
Version 1.2.4 is stable and feature-complete for Java, version 2.x is focused on Scala and doesn't have all the features of 1.2.4 yet.
You don't have to create a servlet. But, you need something that can parse a jsp page - it could be tomcat or some other server which has the same capabilities as tomcat. Though I am not sure if I understood your question correctly.

Categories

Resources