Recently using Java under the WebSocket, there is a requirement, when the page and Java background to establish a connection to get the parameters provided on the page, that is, in the method of @onopen annotations to get the parameters of the page, there is a very simple way to get. Even with @pathparam annotations.
The first step:
The annotations in the Java class that are mapped by the WebSocket are as follows:
@ServerEndpoint ("/websocket/{relationid}/{usercode}")
whichWebSocketis the map address
/{relationid}/{usercode} is two parameters that can be passed to the page, respectively
Step Two:
In the method in the Java class:
This allows you to get the values of the two parameters in the first step (Relationid and Usercode)
Step Three:
In the JavaScript script
var ipport = Window.location.host;
Chat.initialize = function () {if (Window.location.protocol = = ' http: ') {chat.connect (' ws://' + ipport+ '/aninvestigation /websocket/1/2 ');} else {chat.connect (' wss://' + ipport+ '/aninvestigation/websocket/1/2 ');}};
which/ANINVESTIGATION/WEBSOCKET/1/2"1" and "2" are custom parameters.
The 3 steps above will allow you to pass the value of the page into Java at once.
Here's the code and run:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
WebSocket Get page pass-through @pathparam when establishing a connection