How to let browser send messages to a local java application? - java

I want the browser to send a message to my local Java application (which is programmed by myself) when I click a button on a web page which is also written by myself using php. Is there any way I can do that?

This is the wrong way. You don't send information from your server to your local application. Your local application should grab the information from your web server.

The local Java application can implement a webserver. There is a number of libraries out there, or you can use ServerSocket / SSLServerSocket to implement it low level. The server socket should be bound to localhost in order to prevent direct external access.
You can use JSONP to communicate with this local webserver.
Pay special attention to authentication, because any website you visit, can instruct the browser to send requests to the local webserver.

Related

InternetAccess with Jetty and a Webserver. Client-Server Communication 2 machines, computer, java

I need some advice in the following matter:
I have two machines that are connected via ethernet.
One machine (lets call it ServerSide) is also connected to the Internet via LAN. The second machine (ClientSide) is offline, in the beginning.
So ServerSide is creating a webserver with Jetty on port XY. ClientSide opens a webbrowser and requests some page (e.g. stackoverflow.com). The request will be forwarded to port XY and the webserver. ServerSide would have to send the request to the internet and then back over ServerSide to ClientSide, so the webbrowser should display the requested webpage.
Is it even possible to do that this way?
Thanks in advance
Yes. What you need on server side is an HTTP Proxy and there are plenty of existing solutions in the market.
Check out the Wikipedia article about proxies. Bear in mind that the client might require some configuration (Proxy settings) so that it forwards the requests to the proxy rather than attempting to reach the final host.

Uniquely connect an android application to a java applet on pc

I want to connect my android application to an applet which is running on my pc on Google chrome on Wi-fi.. where my phone works as a wi-fi hotspot and pc as the connected device. I want the connection to work uniquely as I want commands to be passed from my application to the specific applet, on the execution of which my applet does specific tasks. Please tell me the APIs which I can look in both Java and Android or the technology I have to use to make it work..
You need to use any program, such as wamp server, to make your computer to be a localserver. It will install PHP 5, MySQL and Apache. In other hand, you will also need a little bit of knowledge in Php language to create you own web services.
Another thing you need to be aware is that to handle you connection between server and device (and by this I mean which IP you are going to use) you will have a little headache; but first things first..break your problem in little parts thus will be easier to solve them.
I recommend this tutorial.
I think the simple way to connect these two softwares is using UDP.
It is fast, it is easy to program but it is generally unreliable according to TCP. But it is already local network. I dont think that is a case you need to take care in your local wifi network.
So take a look at this tutorial http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html
There are other ways like https://www.alljoyn.org/. It has more functinality but more complicated.
You must install Server on your PC(Apache httpd or apache tomcat or other based on your interest). A server listens to request from clients. When your mobile is connected to your pc(doesn't matter wire or wireless), you can make a request to an url(say, localhost:8080/welcome) from your app.
Create an applet and connect it with your web application(in the server) using java.net.URL and java.net.URLConnection.
On performing some operation on the client, call the url of the server application and forward the response to the applet.

Intercommunication between JNLP(client) / Apache / Java program(server)

I am practicing some web-apps technology. I setup Apache HTTP server which provides HTTP page with JNLP/FlashFX file with simple data-form to fill by the user. My first idea was to send/receive the data from JNLP with help of UDP (I just serialize object inside datagrams). To be more specific:
Apache provides a static HTTP and JNLP/FlashFX (HTTP is just to deploy JNLP)
JNLP communicates via UDP with server
server runs simple Java program to send/receive UDP packets to JNLP
My problem is when I access the page from 'client' browser machine a firewall asking if I want to allow/deny the access to network from 'java'. No doubts this is normal, but I think no one is expecting this from a web page.... I want to change this approach and use existing HTTP protocol.
QUESTION UPDATE
As far as I understood with HTTP protocol we have couple of methods which are used to communicate with the server (GET, PUT, POST .... ) and provided by Apache service.
I would like to use this for data exchange like this:
JNLP sends some serialized data with HTTP methods
Apache will redirect some (or full) traffic to my Java program
My Java program will answers via Apache to my JNLP
How can I do that?

Servlet and serial port

We need to develop centralized device management based on java. So Could you tell me how to write servlet to read serial port from client with usb token?
Not easily. In order to access resources on the client outside of the protected "sandbox" in the browser you will have to do it using a signed applet that communicates with the serial port and then back to the server (through some servlet interface like a web-service, for instance)
Alternatively, you can write a "real" client application that you would have to distribute and run on the client machine. This application would, in turn, communicate with the serial port and with the server through some communication method. This could also be a servlet-based communication like a web-service.
Point is, this has very little to do with servlets as a servlet executes on the server and has nothing to do with the client.

OpenPeak Flash app XMLSocket Security Problem

We're trying to create an app for OpenPeak. The Flash app will act as a client to a Java server on another computer on another domain.
The Flash app client connects via XMLSocket. The Java Server uses ServerSocket to receive a request and send back a message.
In order to trust the server, the Flash client needs a socket master policy file to tell it that the server comes from a trustworthy domain. According to an article on Adobe, when a connection has succeeded, the Flash client automatically requests the cross-domain policy or socket master policy file on port 843.
Still, even when we implement it according to the tutorials and recommendations we have read, the Flash client continues to throw the following security error:
SecurityErrorEvent
type="securityError" bubbles=false
cancelable=false eventPhase=2
text="Error #2048"
We've tried logging the policy file request during testing to see if there was any call made to port 843. There was not.
Interestingly, even without a policy file, the Flash client still manages to send the first data message to the server successfully. It's just when the server tries to send back a reply that the entire thing hangs for about 10 seconds before the security error above is displayed.
Any ideas / suggestions?
In order for flash to access another domain you'll need a crossdomain.xml file, For this specific need you will probably have to specify a custom URL. Here is the code you need to do that:
flash.system.Security.loadPolicyFile("{Url to my crossdomain.xml file on the SSL virtual root}");
Here is a sample file which disables flash's "Same Origin Policy" protection for the domain that is using this file. Limiting this access to specific domains is highly recommended, but this is not always possible for some apps.
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*.*" secure="false" />
</cross-domain-policy>
To debug this I would use TamperData to make sure flash is actually downloading the crossdomain.xml file. If you need to debug rtmp or some other non-http protocol then you'll have to use Wireshark.

Categories

Resources