简体   繁体   中英

How a web application can communicate to a standalone Java application

I have created a simple standalone Java application using swing that is used to read the data from OMNIKEY® 3121 card reader and I must send the scanned user details to a web application.

I am sending this data to the web application database using RESTful Java client. But i need to pass the Session ID of the web user to server along with the scanned user details for identifying the card scan request given by which web user. For that i need to receive the request at the Java application with Session ID from the server first. So how can I make this communication possible.

It does not seem to be possible without implementing the registration of the running instance of your swing application in the web application. Then, the swing application could establish WebSocket connection to to web application and subscribe for receiving notifications about actions of the web app users.

1) After the swing app started, it establishes the WebSocket connection with the web application, using some unique ID like domain/cardreader ID.

2) Accounts of the web application users must be linked to that unique ID.

3) With this, the swing app could receive notifications about, for example, sign in events of users, linked to it.

If your requirement is using completely independent swing application instances and user accounts, you can use application links (user clicks this link, and the browser runs your swing application with this url). This article may be good start.

Create a InetSocketAddress from the Desktop Java application using com.sun.net.httpserver.HttpServer as shown below

try {
        HttpServer hs = HttpServer.create(new InetSocketAddress(8888), 0);
        hs.createContext("/test", new HttpHandler() {
                public void handle(HttpExchange t) throws IOException {
                InputStream is = t.getRequestBody();
                System.out.println(t.getRemoteAddress().toString());
                String response = "HelloWorld!!";
                t.sendResponseHeaders(200, response.length());
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
        });
        hs.setExecutor(null);
        hs.start();
    } catch (IOException e) {
        e.printStackTrace();
   }

Once you create the socket you can hit the handle method using http://localhost:8888/test . If you try to make a cross domain access the request may block by the CORS Policy . In that case you have to set the Access control related parameters like shown below before sendResponseHeaders .

            Headers headers = t.getResponseHeaders();
            headers.add("Access-Control-Allow-Headers","x-prototype-version,x-requested-with");
            headers.add("Access-Control-Allow-Methods","GET,POST");
            headers.add("Access-Control-Allow-Origin","*");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM