If you have any questions, suggestions, or if you want an e-mail when you can download your own copy of Pulsar, don't hesitate to
contact us!
Call method on server
This example shows how simple it is to call a Java-method from Javascript. By using the annotation "@PulsarMethod" on a Java-method you make the method callable from the browser. The attribute "JavaScript=true" makes Pulsar create a Javascript-method that handles the communication with the server. The Javascript-functions is included in the HTML-template with the "/pulsar/js" URL.
Java-source
@PulsarMethod(JavaScript = true) public String hello() { return "Hello! I'm using Pulsar version " + Core.getPulsarVersion(); }
This example show how you can send information to the server just by passing it as an argument to the Javascript-function. The Javascript-function is created by Pulsar to match the Java-function.
Your name:
Java-source
@PulsarMethod(JavaScript = true) public String checkName(String name) { return "Hello " + name + ", your name has " + name.length() +" characters" + ", and your hashCode is " + name.hashCode(); }
This is an example of an asynchronous call from the browser to the server. When you annotate a Java-function, Pulsar actually creates two Javascript functions, one that is called syncronous, and one that is called asynchronous. For the java-function foo(), Pulsar creates the Javascript functions foo() and fooASync(). This example showes how the browser is frozen when you call a slow function synchronous. If you call the function asyncronous, you don't notice anything.
<script type="text/javascript" src="/pulsar/js/"></script> ... <script type="text/javascript"> var flip = true; function flipText() { var element = document.getElementById('flip'); if (flip) element.innerHTML = 'This text is changed by a Javascript function'; else element.innerHTML = 'Changed text!'; flip = !flip; setTimeout("flipText()", 1000); } setTimeout("flipText()", 1000); </script> ... <input type="button" onclick="slowFunc()" value="Call slow method syncronous"/> <input type="button" onclick="slowFuncASync()" value="Call slow method asyncronous"/> <label id="flip"> </label>
Call method on server asynchronous with a callback function
This example shows how you can get the result back when you call an asynchronus method. You simply add an extra parameter with a function that should be called with the result from the Java-function.
Java-source
@PulsarMethod(JavaScript = true) public String prime() { long l_startTime = System.currentTimeMillis(); while (System.currentTimeMillis() < l_startTime + 5000) { prime = prime.nextProbablePrime(); } return "" + prime + " is probable prime..."; }
Current time on the server is: <pulsar:method method="getTime"/> This method has been called <pulsar:method method="getMethodCount"/> times
Dependent drop-downs
This is a classic example of dependent drop-downs. The left example shows the old school-solution, that reloads the whole page when you select an item. The right example shows how Pulsar can reload just a part of the page. This makes the solution faster, without any unnecessary scrolling of the page.
Country
Region
City
Java-source
@PulsarMethod public XMLRepresentation getCountries() { return new XMLConverter(countries); }
@PulsarMethod public XMLRepresentation getRegions(Long countryId) { if (countryId == null) return null; Country l_country = countries.get(countryId); return new XMLConverter(l_country.regions); }
@PulsarMethod public XMLRepresentation getCities(Long regionId) { if (regionId == null) return null; Region l_region = regions.get(regionId); return new XMLConverter(l_region.cities); }
Note: In this example we use the function $() and $F() from the Javascript Framework
prototype. $() is just a
shortcut of document.getElementById() and $F() returns the value of any field input control.