JQuery with Jersey on GAEj

This tutorial was made to help my brother use JQuery to make rest call to Jersey based resources. I thought why not post it out here. Maybe it will help someone out there.

Sanbox Environment

  • OS: Windows Vista
  • IDE: Eclipse Galileo
  • Google App Engine 1.3.1
  • Jersey 1.1.5.1
  • JQuery 1.4.2

Setting Up:

Start a Google App Engine project in Eclipse without GWT. Download JQuery library and put it under “war” folder. Download Jersey Bundle 1.5.1 from here. You would also need to download asm-3.1.jar from here and JSR311 from here.

web.xml

Set up Jersey configuration in web.xml as follow. I’m using Main.java as my configuration java file to setup resources. Also note servlet mapping portion of this web.xml. All request sent to http://<yourdomain.com>/rest/* will be routed to Jersey for interception.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; version=&quot;2.5&quot;&gt;
	&lt;servlet&gt;
		&lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
		&lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
			&lt;param-value&gt;com.dclonline.samples.jerseyserver.Main&lt;/param-value&gt;
		&lt;/init-param&gt;
		&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
	&lt;/servlet&gt;

	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;
&lt;/web-app&gt;

Main

Main.java file is just defining one resource for this tutorial. You can define as many resources here as you want.

package com.dclonline.samples.jerseyserver;
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class Main extends Application {
	public Set&lt;Class&lt;?&gt;&gt; getClasses() {
		Set&lt;Class&lt;?&gt;&gt; s = new HashSet&lt;Class&lt;?&gt;&gt;();
		s.add(MockTaxResource.class);
		return s;
	}
}

MockTaxResource

This is my sole java resource file. For this JQuery test I have only defined two resources, TaxInfo in XML resource and TaxInfo in JSON resource.

package com.dclonline.samples.jerseyserver;

import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path(&quot;/taxinfo&quot;)
public class MockTaxResource {
	Logger log = Logger.getLogger(MockTaxResource.class.getName());
	//private Injector injector;

	@GET
	@Produces(&quot;text/plain&quot;)
	public String getHello(){
		return &quot;hello&quot;;
	}

	@GET
	@Path(&quot;{tin}/xml&quot;)
	@Produces(&quot;text/xml&quot;)
	public String getTaxInfoByTinXML(@PathParam(&quot;tin&quot;) String tin) {
		StringBuffer stbuf = new StringBuffer();
		TaxDetail taxDetail = new MockTaxDAO().getTaxDetailByTaxId(tin);
		if(taxDetail != null){
			stbuf.append(&quot;&lt;taxinformation&gt;&lt;nameofcitizen&gt;&quot;);
			stbuf.append(taxDetail.getNameOfCitizen());
			stbuf.append(&quot;&lt;/nameofcitizen&gt;&lt;taxid&gt;&quot;);
			stbuf.append(taxDetail.getTaxId());
			stbuf.append(&quot;&lt;/taxid&gt;&lt;totaltax&gt;&quot;);
			stbuf.append(taxDetail.getTotalTax());
			stbuf.append(&quot;&lt;/totaltax&gt;&lt;paidtax&gt;&quot;);
			stbuf.append(taxDetail.getPaidTax());
			stbuf.append(&quot;&lt;/paidtax&gt;&lt;/taxinformation&gt;&quot;);
			return stbuf.toString();
		}else{
			return &quot;&lt;taxinformation&gt;&lt;nameofcitizen&gt;&quot; +
					&quot;Empty Citizen Found&quot; +
					&quot;&lt;/nameofcitizen&gt;&lt;taxid&gt;&quot; +
					&quot;Empty TIN&quot; +
					&quot;&lt;/taxid&gt;&lt;totaltax&gt;&quot; +
					&quot;000.00&quot; +
					&quot;&lt;/totaltax&gt;&lt;paidtax&gt;&quot; +
					&quot;000.00&quot; +
					&quot;&lt;/paidtax&gt;&lt;/taxinformation&gt;&quot;;
		}
	}

	@GET
	@Path(&quot;{tin}/json&quot;)
	@Produces(&quot;application/json&quot;)
	public String getTaxInfoByTinJSON(@PathParam(&quot;tin&quot;) String tin) {
		StringBuffer stbuf = new StringBuffer();
		TaxDetail taxDetail = new MockTaxDAO().getTaxDetailByTaxId(tin);

		if(taxDetail != null){
			stbuf.append(&quot;{&quot;taxinformation&quot;:{&quot;nameofcitizen&quot;:&quot;&quot;);
			stbuf.append(taxDetail.getNameOfCitizen());
			stbuf.append(&quot;&quot;,&quot;taxid&quot;:&quot;&quot;);
			stbuf.append(taxDetail.getTaxId());
			stbuf.append(&quot;&quot;,&quot;totaltax&quot;:&quot;&quot;);
			stbuf.append(taxDetail.getTotalTax());
			stbuf.append(&quot;&quot;,&quot;paidtax&quot;:&quot;&quot;);
			stbuf.append(taxDetail.getPaidTax());
			stbuf.append(&quot;&quot;}}&quot;);
			return stbuf.toString();
		}else{
			return &quot;{&quot;taxinformation&quot;:{&quot;nameofcitizen&quot;:&quot;&quot;+
					&quot;Empty Citizen Found&quot; +
					&quot;&quot;,&quot;taxid&quot;:&quot;&quot; +
					&quot;Empty TIN&quot; +
					&quot;&quot;,&quot;totaltax&quot;:&quot;&quot; +
					&quot;000.00&quot; +
					&quot;&quot;,&quot;paidtax&quot;:&quot;&quot; +
					&quot;000.00&quot; +
					&quot;&quot;}}&quot;;
		}

	}
}

MockTaxDAO

package com.dclonline.samples.jerseyserver;

public class MockTaxDAO {
	public TaxDetail getTaxDetailByTaxId(String pTaxId) {
		if (pTaxId.equals(&quot;TIN2222&quot;)){
			return new TaxDetail(pTaxId,&quot;Zama Uddin Ahmed&quot;,5000,2000);
		}else if(pTaxId.equals(&quot;TIN0000&quot;)){
			return new TaxDetail(pTaxId,&quot;Bijoy Bishash&quot;,4000,0);
		}else if(pTaxId.equals(&quot;TIN1111&quot;)){
			return new TaxDetail(pTaxId,&quot;Sharif Siddique&quot;,1000,1000);
		}else{
			return null;
		}
	}
}

TaxDetail


JQuery Client

&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;SampleJqueryClient&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;h1&gt;Sample JQuery client with Jersey!&lt;/h1&gt;

&lt;table&gt;
	&lt;tr&gt;
		&lt;td colspan=&quot;2&quot; style=&quot;font-weight: bold;&quot;&gt;
		Get tax information using Tax ID
		&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td colspan=&quot;2&quot; style=&quot;font-weight: bold;&quot;&gt;
			Valid test values are : TIN0000 , TIN1111 , and TIN2222
		&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; &lt;input type=&quot;text&quot; id=&quot;inputTaxId&quot; value=&quot;TIN0000&quot;&gt;&lt;/input&gt; &lt;/td&gt;
		&lt;td&gt; &lt;input type=&quot;button&quot; id=&quot;getTaxInfoByTaxIdXML&quot; value=&quot;Get Tax Info in XML&quot;&gt;&lt;/input&gt; &lt;/td&gt;
		&lt;td&gt; &lt;input type=&quot;button&quot; id=&quot;getTaxInfoByTaxIdJSON&quot; value=&quot;Get Tax Info in JSON&quot;&gt;&lt;/input&gt; &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;
			&lt;div id=&quot;result&quot;&gt;Tax Result Display DIV&lt;/div&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {

		/*
		There is probably better way of Event handling to capture Button click . I'll investigate that later.
		Right now we will have a lot of code repitation. One for XML and one for JSON on the client side.
		I've included both XML and JSOn for you to see the difference.
		*/
	   $(&quot;#getTaxInfoByTaxIdXML&quot;).click(function(){

		var inputTaxId = $(&quot;#inputTaxId&quot;).val();
		if(inputTaxId.substr(0,3)!=&quot;TIN&quot; || inputTaxId.length!=7){
			/*
			do some kind of validation. i.e. Check first three characters are &quot;TIN&quot; or &quot;tin&quot;.
			If not then block sending request to server
			*/
			alert(&quot;Enter a valid tin id&quot;);
		}else {
			$(&quot;#result&quot;).html(&quot;Getting data wait please.........&quot;);
			var urlwithtin = &quot;/rest/taxinfo/&quot;+inputTaxId+&quot;/xml&quot;;
			$.ajax({
		        type: &quot;GET&quot;,
				//url: &quot;/rest/taxinfo/tin3333/xml&quot;,
				url: urlwithtin,
				dataType: &quot;xml&quot;,
				success: function(xml) {
			 		$(xml).find('taxinformation').each(function(){
				 		var name = $(this).find('nameofcitizen').text();
						var taxid = $(this).find('taxid').text();
						var totaltax = $(this).find('totaltax').text();
						var paidtax = $(this).find('paidtax').text();
						$(&quot;#result&quot;).html(&quot;&lt;table&gt;&quot;
                                + &quot;&lt;tr&gt;&lt;td&gt;Name:  &lt;/td&gt;&lt;td&gt;&quot;+name + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                                + &quot;&lt;tr&gt;&lt;td&gt;Tax ID:   &lt;/td&gt;&lt;td&gt;&quot;+taxid+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                                + &quot;&lt;tr&gt;&lt;td&gt;Total Tax:&lt;/td&gt;&lt;td&gt;&quot;+totaltax+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                                + &quot;&lt;tr&gt;&lt;td&gt;Paid Tax: &lt;/td&gt;&lt;td&gt;&quot;+paidtax+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                                + &quot;&lt;/table&gt;&quot;
                                );
					});
				}
		   	});
		}
	   });

	   $(&quot;#getTaxInfoByTaxIdJSON&quot;).click(function(){

			var inputTaxId = $(&quot;#inputTaxId&quot;).val();
			if(inputTaxId.substr(0,3)!=&quot;TIN&quot; || inputTaxId.length!=7){
				alert(&quot;Enter a valid tin id&quot;);
			}else {
				$(&quot;#result&quot;).html(&quot;Getting data wait please.........&quot;);
				var urlwithtin = &quot;/rest/taxinfo/&quot;+inputTaxId+&quot;/json&quot;;
				$.ajax({
			        type: &quot;GET&quot;,
					//url: &quot;/rest/taxinfo/TIN1111/json&quot;,
					url: urlwithtin,
					dataType: &quot;json&quot;,
					success: function(json) {
						$(&quot;#result&quot;).html(&quot;&lt;table&gt;&quot;
                               + &quot;&lt;tr&gt;&lt;td&gt;Name:  &lt;/td&gt;&lt;td&gt;&quot;+json.taxinformation.nameofcitizen + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                               + &quot;&lt;tr&gt;&lt;td&gt;Tax ID:   &lt;/td&gt;&lt;td&gt;&quot;+json.taxinformation.taxid+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                               + &quot;&lt;tr&gt;&lt;td&gt;Total Tax:&lt;/td&gt;&lt;td&gt;&quot;+json.taxinformation.totaltax+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                               + &quot;&lt;tr&gt;&lt;td&gt;Paid Tax: &lt;/td&gt;&lt;td&gt;&quot;+json.taxinformation.paidtax+&quot;&lt;/td&gt;&lt;/tr&gt;&quot;
                               + &quot;&lt;/table&gt;&quot;
                        );
					}
			   	});
			}
	   });
});
 &lt;/script&gt;
&lt;/html&gt;

Add a Comment

Your email address will not be published. Required fields are marked *