Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 48 total  RSS 

Method dependent Phobos server side script

Method dependent Phobos server side script

   1  
   2  // method-dependent script
   3  
   4  var message;
   5  
   6  library.httpserver.onMethod({
   7      GET: function() { message = "accessed via GET"; },
   8      POST: function() { message = "received POST"; },
   9      any: function() { message = "got something else"; },
  10  });
  11  
  12  model = { text: message };

Printing all http headers in Phobos JavaScript server side

Printing all http headers in Phobos JavaScript server side

   1  
   2  // prints all http headers
   3   
   4  function get_headers() {    
   5      var a = []
   6      var hEnum = request.headerNames;
   7      while (hEnum.hasMoreElements()) {
   8          var n = hEnum.nextElement();
   9          a.push({name: n, value: request.getHeader(n)});
  10      }
  11      return a;
  12  }
  13  
  14  response.contentType = 'text/html';
  15  writer = response.writer;
  16  
  17  writer.println('<html><head><title>Sample header script</title></head><body>');
  18  var headers = get_headers();
  19  for (var i = 0; i < headers.length; ++i) {
  20      var header = headers[i];
  21      writer.print(header.name);
  22      writer.print(' => ');
  23      writer.print(header.value);
  24      writer.println('<br/>');
  25  }
  26  writer.println('</body></html>');
  27  writer.flush();
  28  

Dojo usage on the Phobos server side...

Dojo usage on the Phobos server side...
The key api is the library.dojo.load() call

   1  
   2  // server-side dojo test script
   3  
   4  response.status = 200;
   5  response.contentType = "text/html";
   6  writer = response.getWriter();
   7  writer.println("<html><head><title>Dojo Sample</title></head><body>");
   8  
   9  library.dojo.load();
  10  dojo.require("dojo.lang.*");
  11  try {
  12      dojo.lang.assert (1==2," 1 is equals to 1");
  13      writer.println("OK: 1 is equals to 1");
  14      }catch (e){
  15      writer.println(e.message);
  16      
  17  }
  18  dojo.require("dojo.date.common");
  19  
  20  writer.println( "<br>dojo.date.getWeekOfYear returns "+dojo.date.getWeekOfYear(new Date()));
  21  writer.println("</body></html>");
  22  writer.flush();
  23  
  24  

E4X JavaScript sample in Phobos server side Javascript

E4X JavaScript sample in Phobos server side Javascript

   1  
   2  
   3  // see var below: no "", but pure xml:
   4  var e4x = <html><head><title>Hello</title></head><body>Hello from Javascript!</body></html>;
   5  response.setStatus(200);
   6  response.setContentType("text/html");
   7  writer = response.getWriter();
   8  writer.println(e4x);
   9  
  10  // another example for e4x
  11  var sales = <sales shoppingcarID="12345">
  12      <item type="LCD TV" price="3000" quantity="5"/>
  13      <item type="Syno PSP 3" price="3" quantity="0"/>
  14      <item type="Book E4 for dummies" price="35" quantity="3"/>
  15    </sales>;
  16   
  17  writer.println( "<br>quantity for Book E4 for dummies: "+sales.item.(@type == "Book E4 for dummies").@quantity );
  18  writer.println("<br>" );
  19  writer.println( "shopping cart ID is "+sales.@shoppingcarID);
  20  writer.println("<br>" );
  21  for each( var price in sales..@price ) {
  22      writer.println("<br>price is: "+ price );
  23  }
  24  
  25  writer.flush();

JPA (Java Persistence API) usage in Phobos Server side JavaScript

JPA (Java Persistence API, Java EE 5) usage in Phobos Server side JavaScript

   1  
   2  
   3      response.setStatus(200);
   4      response.setContentType("text/html");
   5      writer = response.getWriter();
   6  
   7      writer.println("<html><head><title>Using JPA in Phobos JavaScript</title>");
   8      writer.println("</head><body><center><h2>Java Persistence API usage in Javascript.</h2></center>");
   9  
  10      // get the entity manager based on the Persistence Unit name (declared in the persistence.xml file)
  11      var em;
  12      try {
  13          var emf = Packages.javax.persistence.Persistence.createEntityManagerFactory("jpaExample1-pu", null);
  14          em = emf.createEntityManager();
  15      } catch (exception) {
  16          var exceptionMsg = exception.description;
  17          if (exception.description == null) {
  18              exceptionMsg = exception.message;
  19          }                            
  20          if (exceptionMsg.indexOf("DatabaseException") != -1) {
  21              writer.println("<p><br><br>Please ensure that the database is up and running");
  22          } else if (exceptionMsg.indexOf("NullPointerException") != -1) {
  23              writer.println("<p><br><br>Please ensure that the JPA application is built and the jar is present in the classpath of the phobos server");
  24          }
  25      }
  26      
  27      if (em != undefined) {
  28          // Insert some Authors if none are defined
  29          var authors = em.createQuery("select a from Author a").getResultList().toArray();
  30          if (authors.length==0){
  31              var tx = em.getTransaction();
  32              tx.begin();
  33  
  34              var author = new Packages.jpaexample.Author();
  35              author.name ="Danny Goodman"; //equivalent to author.setName("Danny Goodman");
  36              author.organisation= "O\'Reilly";
  37              em.persist(author);
  38              writer.println("<br><br>created one author named "+ author.name);
  39  
  40              var author2 = new Packages.jpaexample.Author();
  41              author2.name= "Paul Wilton";
  42              author2.organisation ="Wrox Press Inc";
  43              em.persist(author2);
  44              writer.println("<br><br>created one author named "+ author2.name);
  45  
  46              tx.commit();
  47          }
  48  
  49          // List out the Authors that are available
  50          authors = em.createQuery("select a from Author a").getResultList().toArray();
  51          writer.println("<h3>List of Authors as of "+ java.util.Date() +"</h3>")
  52          writer.println("<table><tr><th>Id</th><th>Name</th><th>Organisation</th></tr>")
  53          for (var i in authors) {
  54              var a = authors[i];
  55              writer.println("<tr><td>" + a.authorId
  56              + "</td><td>" + a.name
  57              + "</td><td>" + a.organisation
  58              + "</td></tr>");
  59          }
  60  
  61          writer.println("</table>")
  62      }
  63      
  64      writer.println("</body></html>");
  65      writer.flush();
  66  
  67  

Yahoo Tree jMaki widget for Phobos

Yahoo Tree jMaki widget for Phobos

   1  
   2  <% 
   3   library.jmaki.insert(
   4      {
   5      component : "yahoo.tree" ,
   6      value : {
   7          root : {
   8                  title : 'Default Tree Root Node',
   9                  expanded : true,
  10                  children : [
  11                      { 
  12                          title:'Node 1.1',
  13                          onclick :{url:'foo'}
  14                      },
  15                      { 
  16                          title :'Node 1.2',
  17                          children :[
  18                              { 
  19                                  title :'Node 3.1'
  20                              }
  21                          ]
  22                      }
  23                  ]
  24          }
  25          }   
  26      }
  27      );%> 
  28  

Yahoo Map jMaki widget for Phobos

Yahoo Map jMaki widget for Phobos

   1  
   2  <% library.jmaki.insert(
   3      {
   4          component : "yahoo.map",
   5          args : { 
   6              centerLat :  37.39316, 
   7              centerLon :  -121.947333700, 
   8              mapType :    'YAHOO_MAP_SAT', 
   9              width :      600, 
  10              height :     300
  11              }
  12      }
  13      ); %>
  14  

Yahoo geocoder jMaki widget for Phobos

Yahoo geocoder jMaki widget for Phobos

   1  
   2  <% library.jmaki.insert(
   3      {
   4          component : "yahoo.geocoder",
   5          service : "xhp"
   6      }
   7      ); %> 
   8  
   9  <script  type:"text/javascript">
  10      function geoCoderListener(coordinates) {
  11      var targetDiv = document.getElementById("geocoder001_message");
  12      var reponseText  = "";
  13      for (var i in coordinates) {
  14          reponseText += "Latitude=" + coordinates[i].latitude + " Longitude=" +  coordinates[i].longitude + "<br>";
  15      }
  16      targetDiv.innerHTML = reponseText;
  17      }
  18      // subscribe to the topic '/yahoo/geocode' to which this widget publishes events
  19      jmaki.subscribe("/yahoo/geocoder", geoCoderListener);
  20  </script>
  21  <div id="geocoder001_message"></div>
  22  

Yahoo Carousel jMaki widget for Phobos

Yahoo Carousel jMaki widget for Phobos

   1  
   2  <% library.jmaki.insert(
   3      {
   4          component : "yahoo.carousel",
   5          args : {
   6                  tags : 'theKt'
   7          } 
   8      }
   9      ); %> 

Yahoo Calendar jMaki widget for Phobos

Yahoo Calendar jMaki widget for Phobos

   1  
   2  <% library.jmaki.insert(
   3      {
   4          component : "yahoo.calendar" 
   5      }
   6      ); %> 
   7  
« Newer Snippets
Older Snippets »
Showing 1-10 of 48 total  RSS