Wednesday, May 22, 2013

Object-Oriented JavaScript

JavaScript Today

JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers, PCs, laptops, tablets, smart phones, and more .

JavaScript is a prototype-based scripting language that is dynamic, weakly typed, and has first-class functions. Its syntax was influenced by the language C. JavaScript copies many names and naming conventions from Java, but the two languages are otherwise unrelated and have very different semantics. The key design principles within JavaScript are taken from the Self and Scheme programming languages.It is a multi-paradigm language, supporting object-oriented,imperative, and functional programming styles.

JavaScript's use in applications outside of web pages—for example, in PDF documents, site-specific browsers, and desktop widgets—is also significant. Newer and faster JavaScript VMs and frameworks built upon them (notably Node.js) have also increased the popularity of JavaScript for server-side web applications.
JavaScript was formalized in the ECMAScript language standard and is primarily used as part of a web browser (client-side JavaScript). This enables programmatic access to computational objects within a host environment.

Object Orientation 

Thursday, May 16, 2013

HTML5 Graph Drawing with rgraph


RGraph is a HTML5 charts library that uses the HTML5 canvas tag to draw and supports over twenty different types of charts. Using the new tag, RGraph creates these "HTML charts" inside the web browser using JavaScript, meaning quicker pages and less web server load. This leads to smaller page sizes, lower costs and faster websites.

Forget Flash or Silverlight - JavaScript and now HTML5 canvas are built in to all modern browsers and allow for quick and easy 2D drawing. Using RGraph to produce your charts makes the process a breeze and enables you to quickly get up and running. HTML5 Canvas is supported by all modern browsers and mobile devices meaning your charts and graphs will be seen by the widest possible number of users. You can read more about the canvas tag here as well as more about JavaScript charts here.

Some Example of the Types of Graphs that can be drawn.


Bar charts


Line charts





Gauge charts





http://www.rgraph.net/examples/gauge.html

Consuming a RESTful Service with jQuery.get()

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.


jQuery.get()

Loads data from the server using a HTTP GET request.



Examples:

Example: Request the test.php page, but ignore the return results.

1
$.get("test.php");

Example: Request the test.php page and send some additional data along (while still ignoring the return results).

1
$.get("test.php", { name: "John", time: "2pm" } );

Example: Pass arrays of data to the server (while still ignoring the return results).

1
$.get("test.php", { 'choices[]': ["Jon", "Susan"]} );

Example: Alert the results from requesting test.php (HTML or XML, depending on what was returned).

1
2
3
$.get("test.php", function(data) {
alert("Data Loaded: " + data);
});

Example: Alert the results from requesting test.cgi with an additional payload of data (HTML or XML, depending on what was returned).

1
2
3
4
$.get("test.cgi", { name: "John", time: "2pm" })
.done(function(data) {
alert("Data Loaded: " + data);
});

Example: Get the test.php page contents, which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>), and add it to the page.

1
2
3
4
5
$.get("test.php",
function(data) {
$('body').append( "Name: " + data.name ) // John
.append( "Time: " + data.time ); // 2pm
}, "json");
 

Note : 

As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed theXMLHttpRequest object). However, since JSONP and cross-domain GET requests do not use XHR, in those cases thejqXHR and textStatus parameters passed to the success callback are undefined.

Creating a Java Enterprise Application with EE 5 and JBoss 5.1 EAP

Steps for Creating a Java Enterprise Application with EE 5 and JBoss 5.1 EAP

  1. Install Netbeans
  2. Install JBoss and configure
  3. Create the Enterprise Application
  4. Create a Session Bean
  5. Create a REST service from patterns\
  6. Clean and Build the Project
  7. Run the Server Manually
  8. Deploy the Enterprise Archive
  9. Access the Service via HTTP



Install Netbeans

Here's a link on how to innstall the Netbeans IDE in your machine.

Install JBoss and configure

You can learn about the installation of JBoss for our previous post at http://ifsinternz.blogspot.com/2013/05/installing-jboss-on-windows-environment_6.html

Create the Enterprise Application

Open the IDE and select Java EE --> Enterprise Application


Then name and configure your application by selecting your Server and Java EE Version.Also if you want an instance different from the Default one , you can also set the port and instances.





Finally Finish the setup.


Create a Session Bean

Then we will add a session Bean to keep the Business logic of the Application.


Then you can create the business logic and then we will see how the IDE will auto generate the interface for the method that we define.


Create a REST service from patterns

Now to use the EJB for the RESTful web service we will create a new RESTful Web Service from Patterns.This will just be a service providing root resources.
You can define the type of MIME you want and the output Representation.





Lets now define the EJB instance in the new REST Service we created and produce the output of it in the get HTML method for the consumer to consume.






Clean and Build the Project


In Netbeans you can very easily deploy the project with the Server and run the instance.But as a developer it is a good practice to seperate the process of deployment from the IDE.That way you can easily debug and find errors that are sometimes much more time consuming to find using the IDE.


So we will use the clean and Build at the IDE to Generate the .ear file and manually deploy it in the Server.


Deploy the Enterprise Archive

To deploy the enterprise application to JBoss you can copy the .ear from the netbeans project ( It will be at the dist folder of the project) and paste it directly in the deploy folder of the JBoss Installation instance.
COPY:





PASTE:
















Run the Server Manually

To run the server manually you can just use the run command at the bin directory.But if you use an instance other than the default instance of the server you have to tell it to the server by using the run -c <your_instance_name>  command.  
 



Access the Service via HTTP




extra : Class cast error
Library mismatch