0

I am new to java. I have a task to complete.
I have two Html files

page1.html

<html>
    <head>
    </head>
    <body>
        <a onclick="send('hello');"><li>Hello</li></a>
        <script>
            var ws;
            ws = new WebSocket("ws://localhost:8080/Test/actions");
            function send(text){
                ws.send(text);
            }    
        </script>
    </body>
</html>

I want to show this text in other html file (Page2.html), when it is clicked in Page1.html. And I have to implement it using Java WebSocket Only.

ric
  • 627
  • 1
  • 12
  • 23
user21
  • 23
  • 2
  • 9

1 Answers1

0

First you need to implement websocket callback method in your first html

    <script>
              var ws;
              ws = new WebSocket("ws://localhost:8080/Test/actions");
              function send(text){
                 ws.send(text);
               }

              ws.on('message', function incoming(data) {
               console.log(data);
              url = 'http://your_html_file_path/second.html?value='+ 
                     encodeURIComponent(data);
               document.location.href = url;

               });    
         </script>

Then you can try below methods to pass your data to another page

 url = 'http://your_html_file_path/second.html?value='+ encodeURIComponent(data);
 document.location.href = url;

There are different ways. Please check below answers too.

Passing Variable through JavaScript from one html page to another page

Transfer data from one HTML file to another

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39