how to run java on website and to get values to html -


i know question may sound easy of stuck it.

first of define trying achieve. on eclipse running piece of code sends data on specific port, , via html , javascript getting it's sent , print them on screen.

i have account 1 of free hosting websites. want run code on website e.g mywebsite.blahblah.com/... , html file on computer want access website, values produced java code , print them on screen.

i have no idea start. codes are

java , html

import java.net.inetsocketaddress; import java.net.unknownhostexception; import java.util.collection; import org.java_websocket.websocket; import org.java_websocket.websocketimpl; import org.java_websocket.handshake.clienthandshake; import org.java_websocket.server.websocketserver;  public class gpsserver extends websocketserver {  static int port = 9876;  public gpsserver(int port) throws unknownhostexception {     super(new inetsocketaddress(port)); }  public gpsserver(inetsocketaddress address) {     super(address); }  public void senddata(string s) {      collection<websocket> con = connections();     synchronized (con) {         (websocket c : con) {             c.send(s);         }     } }  @override public void onopen(websocket arg0, clienthandshake arg1) {     system.out.println(arg0.getremotesocketaddress().getaddress()             .gethostaddress()             + " connected server!"); }  @override public void onclose(websocket arg0, int arg1, string arg2, boolean arg3) {     system.out.println(arg0 + " disconnected!"); }  @override public void onerror(websocket arg0, exception arg1) {     arg1.printstacktrace();     if (arg0 != null) {     } }  @override public void onmessage(websocket arg0, string arg1) {     system.out.println(arg0 + ": " + arg1);  }  public static runnable senddata() {      runnable r = new runnable() {          @override         public void run() {             websocketimpl.debug = true;             gpsserver server;              try {                 server = new gpsserver(gpsserver.port);                 server.start();                  system.out.println("gps server started @ port: "                         + server.getport());                  double longitude = 39.55;                 double latitude = 22.16;                 string lng = double.tostring(longitude);                 string ltd = double.tostring(latitude);                 string = lng + "-" + ltd;                  while (true) {                      server.senddata(all);                     /*                      * server.senddata(double.tostring(longitude));                      * system.out.println("longitude sent...");                      * server.senddata(double.tostring(latitude));                      * system.out.println("latitude sent...");                      */                     thread.sleep(5000);                 }             } catch (unknownhostexception e) {                 e.printstacktrace();             }              catch (interruptedexception e) {                 e.printstacktrace();             }         }     };     return r; }  public static void main(string[] args) throws unknownhostexception {      thread thread = new thread(gpsserver.senddata());     thread.start(); } } 

--

 <!doctype html> <html> <head> <script type="text/javascript"> function websockettest() {  var lat; var lng;   if ("websocket" in window)  {  alert("websocket supported browser!");  console.log("websocket supported browser!");  // let open web socket  var ws = new websocket("ws://localhost:9876/echo");  ws.onopen = function()  {     ws.send("message send");     alert("message sent...");    };         ws.onmessage = function (evt)  {       var partsarray = evt.data.split('-');     lng=partsarray[0];     lat=partsarray[1];     alert(lat);     alert(lng);      };         ws.onclose = function() {       alert("connection closed...");      console.log("connection closed..."); };      }   else   {      alert("websocket not supported browser!");   } }  </script> </head> <body> <div id="sse">   <a href="javascript:websockettest()">run websocket</a> </div> <div> <p id="para"> basic html!</p> </div> </body> </html> 

thanks!

i'm assuming you're new web development. haven't studied code basic idea need server side scripting language jsp(of course jsp because you're using java code). hope know javascript's basic idea use resources on client's end, or load data dynamically. if you're concerned displaying values server client, can simple make servlet print data. following mvc pattern,

  • controller== make servlet handle request made user(i.e. link show data,basically). set model in controller once receive request(you can decide on get/post separately too).
  • model== make abstract representation(class of java) holding data displayed.
  • view== here you'll receive model. in other words, html. can use jsp helpers customize view, basic idea control how data shown user(hence name view). html automatically generated @ run-time , passed user.

again, i'm assuming you're new web development. please let me know if haven't understood question well. enjoy coding.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -