This feature enables the user to generate JavaScript client stubs from RESTful Web Service Resources classes. This makes it easy for client applications to access the RESTful service. To generate client stubs, a user has to invoke the "RESTful Web Service Client" action on a web project. The wizard then allows users to select from a list of projects containing RESTful Web Services. See the high-level component diagram (of generated client stubs from a RESTful Web Service Web Project in Netbeans 6.1) below.
Select defaults, then add 'CustomerDB' project, then click 'Finish'
Below you should see code snippets for CRUD operation onto Customer table in DB using the generated stubs.
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<!-- JS_DECLARE_START - DO NOT REMOVE-->
<script type="text/javascript" src="./rest/Support.js"></script>
<script type='text/javascript' src='./rest/customerdb/CustomerDB.js'></script>
<script type='text/javascript' src='./rest/customerdb/Customers.js'></script>
<script type='text/javascript' src='./rest/customerdb/Customer.js'></script>
<script type='text/javascript' src='./rest/customerdb/DiscountCodes.js'></script>
<script type='text/javascript' src='./rest/customerdb/DiscountCode.js'></script>
<script type='text/javascript' src='./customerapp.js'></script>
<!-- JS_DECLARE_END - DO NOT REMOVE-->
</head>
<body>
<h1>JSP Page</h1>
<script language="Javascript">
//Create a new customer
var newCustomer = newCustomer('1000'+getCustomersSize()+1, 'Scott');
//Add this new customer to the customers. This will persist into the DB
addCustomer(newCustomer);
//Check the DB
alert('New customer name: ' + newCustomer.getName());
//Update this new customer
newCustomer.setName('Tiger');
updateCustomer(newCustomer);
alert('Changed customer name: ' + newCustomer.getName());
//Delete this customer
deleteCustomer(newCustomer);
</script>
</body>
</html>
//Change this url value if necessary
var baseUrl = 'http://localhost:8080/CustomerDB/resources/';
var customersObj;
var discountCodesObj;
function getCustomers() {
if(customersObj == null)
customersObj = new Customers(baseUrl+'customers/');
return customersObj;
}
function getDiscountCodes() {
if(discountCodesObj == null)
discountCodesObj = new DiscountCodes(baseUrl+'discountCodes/');
return discountCodesObj;
}
//<!-- Sample Javascript code to get customer names -->
function getCustomersSize() {
var customersObj = getCustomers();
var customers = customersObj.getItems();
return customers.length;
}
function getCustomerNames() {
var customersObj = getCustomers();
var customers = customersObj.getItems();
var names = [];
for(i=0;i<customers.length;i++) {
var customer = customers[i];
names.push(customer.getName());
}
return names;
}
//<!-- Sample Javascript code to get customer -->
function getCustomer(i) {
var customersObj = getCustomers();
var customer = customersObj.getItems()[i];
return customer;
}
//<!-- Sample Javascript code to create and add a new customer-->
function addCustomer(customer) {
var customersObj = getCustomers();
customersObj.addItem(customer);//add locally
var status = customersObj.flush(customer);//flush new customer to the server
return status;
}
function newCustomer(id, name) {
var customerObj = new Customer(getCustomers().getUri()+id+'/', true);
customerObj.setCustomerId(id);
customerObj.setName(name);
customerObj.setEmail('');
customerObj.setAddressline1('');
customerObj.setAddressline2('');
customerObj.setCity('');
customerObj.setState('');
customerObj.setZip('');
customerObj.setPhone('');
customerObj.setFax('');
customerObj.setCreditLimit('1000');
//var discountCodeObj = new DiscountCode(customerObj.getUri()+'L'+'/', true);
customerObj.setDiscountCode(getDiscountCodes().getItems()[0]);
return customerObj;
}
//<!-- Sample Javascript code to update ith customer-->
function updateCustomer(customer) {
var status = customer.flush();//update changes to the Customer Resource on server
return status;
}
//<!-- Sample Javascript code to update customer-->
function deleteCustomer(customer) {
var customersObj = getCustomers();
var status = customersObj.removeItem(customer);
return status;
}
Download the attached CustomerDB.zip
, CustomerApp.zip
, unzip and deploy to GlassFish AppServer to test the REST Remoting capability.
Rich Internet Applications using NB 6.0 RESTful Web Services
| ClientApp.PNG | ![]() |
23838 bytes |
| ClientApp.png | ![]() |
23838 bytes |
| ClientApp.zip | ![]() |
113249 bytes |
| ClientAppTest.PNG | ![]() |
68224 bytes |
| ClientAppTest.png | ![]() |
41229 bytes |
| ClientStubs.png | ![]() |
98818 bytes |
| ClientStubsWizard.png | ![]() |
119004 bytes |
| CustomerApp.png | ![]() |
36985 bytes |
| CustomerApp.zip | ![]() |
25599 bytes |
| CustomerDB.png | ![]() |
94741 bytes |
| CustomerDB.zip | ![]() |
131747 bytes |
| MusicDB.zip | ![]() |
35581 bytes |
| RESTRemoting.png | ![]() |
48442 bytes |
| TestClient.png | ![]() |
85030 bytes |
| music.zip | ![]() |
30185 bytes |
| musicPlayingSong.PNG | ![]() |
41864 bytes |
| musicPlayingSong.png | ![]() |
41864 bytes |
| musicPlaylists.PNG | ![]() |
33693 bytes |
| musicPlaylists.png | ![]() |
33693 bytes |
| musicdb.sql | ![]() |
3331 bytes |
| remotingWizard.PNG | ![]() |
67438 bytes |
| remotingWizard.png | ![]() |
67438 bytes |