Tag: javascript

Javascript everywhere

I can think of at least two scenarios when you might need to run the same algorithms in both a Javascript and a Java environment. A Javascript client is running in offline mode and needs to run some business logic or complex validation which the server will need to run again later when say the model is persisted and the server needs to verify the consistent valid state of the model, You have several clients which need access to the same algorithms, for example a Javascript based single page application running in the browser and web service which your business partners use which is deployed in a Java EE application server. You could build the algorithm twice: once in Java and once in Javascript but that isn't very friendly in terms of maintenance. You could build it once in just Java and make the client call the server to run the algorithm, but that doesn't work in an offline application like you can build using HTML 5 and AngularJS, and it doesn't make for a very responsive client. So why not build the algorithm just once, using Javascript, and then use the javax.script Java package which first shipped with Java SE 7 and was improved with Java SE 8, in order to execute the algorithm when you need to use it from Java? That is precisely what I asked myself, and so I set about building an example of how to do it. The first thing I considered was deployment…

Read more

Sharing, using Social Media Links

Below is some Javascript which lets users share the current page on several social media sites.  It relies on simple anchor tags wrapping images, like those on the top right of this blog.  You could equally use serverside code to fill the link, just my Blog (Pebble) doesn't seem to let me do that (the template doesn't let you add Java tags to the JSP?!).  Note how some use an escaped URL and others don't. --------------------------- var title = escape("Ant's blog"); var url = escape(window.location.href); var facebookLink = document.getElementById("facebookLink"); facebookLink.href = "http://www.facebook.com/sharer.php?u=" + url + "&t=" + title; var twitterLink = document.getElementById("twitterLink"); twitterLink.href = "http://www.twitter.com/home?status=" + window.location.href; var googleLink = document.getElementById("googleLink"); googleLink.href = "http://www.google.com/bookmarks/mark?op=add&bkmk=" + url + "&title=" + title; var diggLink = document.getElementById("diggLink"); diggLink.href = "http://www.digg.com/submit?phase=2&url=" + window.location.href + "&title=" + title; var myspaceLink = document.getElementById("myspaceLink"); myspaceLink.href = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + url + "&t=" + title + "&c=" + title; var deliciousLink = document.getElementById("deliciousLink"); deliciousLink.href = "http://del.icio.us/post?url=" + url + "&title=" + title; var redditLink = document.getElementById("redditLink"); redditLink.href = "http://reddit.com/submit?url=" + url + "&title=" + title; var stumbleUponLink = document.getElementById("stumbleUponLink"); stumbleUponLink.href = "http://www.stumbleupon.com/submit?url=" + url + "&title=" + title;    

Read more

Node JS and Server side Java Script

Let's start right at the beginning. Bear with me, it might get long... The following snippet of Java code could be used to create a server which receives TCP/IP requests: class Server implements Runnable { public void run() { try { ServerSocket ss = new ServerSocket(PORT); while (!Thread.interrupted()) Socket s = ss.accept(); s.getInputStream(); //read from this s.getOutputStream(); //write to this } catch (IOException ex) { /* ... */ } } } This code runs as far as the line with ss.accept(), which blocks until an incoming request is received. The accept method then returns and you have access to the input and output streams in order to communicate with the client. There is one issue with this code. Think about multiple requests coming in at the same time. You are dedicated to completing the first request before making the next call to the accept method. Why? Because the accept method blocks. If you decided you would read a chunk off the input stream of the first connection, and then be kind to the next connection and accept it and handle its first chunk before continuing with the original (first) connection, you would have a problem, because the accept method blocks. If there were no second request, you wouldn't be able to finish off the first request, because the JVM blocks on that accept method. So, you must handle an incoming request in its entirety, before accepting a second incoming request. This isn't so bad, because you can create the ServerSocket…

Read more