Thursday, May 16, 2013

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