java分布式代写 EIE4108 Distributed Systems and Cloud Computing

Distributed Systems and Cloud Computing (EIE4108)

Lab: RESTful Web Services

 

Expected Outcomes

After finishing this lab, students should be able to build distributed applications based on Java RMI.

 

Assessment Criteria

Team work: At most two persons per group and submit one report for each group (one person per group is also acceptable but will not receive bonus marks).

  • Be able to complete the programs and make them to run on a distributed system.
  • Be able to explain how the program works
  • Clarity of the report
  • Proper use of indentation in the programming codes.

Note: Your report may contain some screen captures demonstrating that you can produce the desired outputs of the programs.

 

Pre-Requisite

You are strong suggested to complete the SOAP-WSDL lab in CF503 or at home before attempting this one. The materials of SOAP-WSDL lab can be downloaded from

http://www.eie.polyu.edu.hk/~mwmak/notes/EIE4108/EIE4108_lab_soap-wsdl.doc

 

Procedures for Setting Up Eclipse

 

  1. Download Tomcat 7.0 from http://158.132.148.85:8080/download/EIE4108/REST/. Note that I have already copied the Jersey and Asm class libraries to the lib/ folder in this copy of Tomcat. Alternatively, you may download Jersey from https://jersey.java.net/.

 

  1. Unzip “apache-tomcat-7.0.23-windows-x86.zip” to your home folder, e.g. H:\EIE4108. If you use Mac or Linux, unzip “apache-tomcat-7.0.39-mac-linux.zip”.

 

  • Start up Eclipse. In Eclipse, select Window à Preferences à Server à Runtime Environment and add Apache Tomcat 7.0 as the server. Make sure you add the version 7.0.23 that you have just unzipped.

 

  1. Create a Dynamic Web Project called “REST” using Dynamic web module version 3.0 and Tomcat v7.0. Press the Next button twice and select “Generate web.xml development descriptor” as shown in the figure below. Then, press the Finish For details, refer the Web Service Lab.

 

  1. Create a Java class called Database and an HTML file “TestMarks.html”. Copy and paste the contents of Fig. 1(a) and Fig. 1(b) to these two files. Format the source code by clicking Source à Format. Beware of Microsoft’s special characters. They are not allowed in HTML files.

 

  1. Add the following XML statements to the file “WebContent/WEB-INF/web.xml”. It tells Tomcat that it is going to be a RESTful web service with URL pattern /database/*

 

<web-app . . .>

. . .

<servlet>

<servlet-name>Jersey REST Service</servlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer

</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Jersey REST Service</servlet-name>

<url-pattern>/database/*</url-pattern>

</servlet-mapping>

<web-app

 

  • Start the RESTful service and run the client by right-clicking “TestMarks.html” and select Run As à Run on Server.

 

 

Q1. Fig. 1(a) shows a Java program that provides a RESTful web service. The service allows RESTful clients to add, update, and query the marks of students using student IDs as the keys. Fig. 1(b) shows an HTML file that uses AJAX to access the service. Complete the code so that the screenshots shown in Fig. 1(c) can be obtained. To demonstrate that the program is written by you, you need to replace the student number in Fig. 1(c) with your student number.

 

 

 

 

// Database.java

@Singleton

@Path(“/mark”)

public class Database {

private Hashtable<String,Integer> table = new Hashtable<String,Integer>();

 

public String addRecord(. . .) {

// Add a record using id as key and mark as value

}

 

public String getMark(. . .) {

// Retrieve a record using id as key

}

 

public String updateRecord(. . .) {

// Update a record using id as key and mark as value

 

}

}

 

Fig. 1(a)

 

 

<!- – TestMarks.html – ->

<html>

<body>

<script type=“text/javascript”>

function createRequest() {

var xmlhttp;

if (window.XMLHttpRequest) {

xmlhttp = new XMLHttpRequest();

} else {

xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);

}

return xmlhttp;

}

 

function addRecord() {

var req = createRequest();

var id = document.getElementById(“ID”);

var mark = document.getElementById(“Mark”);

var status = document.getElementById(“Status”);

var url = “http://localhost:8080/REST/database/mark/” + id.value +

“/” + mark.value;

req.onreadystatechange=function() {

if (req.readyState==4 && req.status==200) {

if (req.responseText == “”) {

status.value = “Record of ” + id.value + ” exists”;

} else {

status.value = “Record of ” + id.value + ” added”;

}

}

}

req.open(POST, url, true);

req.send();

}

function getRecord() {

var req = createRequest();

var id = document.getElementById(“ID”);

var mark = document.getElementById(“Mark”);

var url = “http://localhost:8080/REST/database/mark/” + id.value;

var status = document.getElementById(“Status”);

req.onreadystatechange = function() {

if (req.readyState == 4 && req.status == 200) {

if (req.responseText == “”) {

status.value = “Record of ” + id.value + ” not exists”;

mark.value = “”;

} else {

status.value = “Mark of ” + id.value + “: ” +

req.responseText;

}

}

}

req.open(“GET”, url, true);

req.send();

}

 

function updateRecord() {

var req = createRequest();

var id = document.getElementById(“ID”);

var mark = document.getElementById(“Mark”);

var status = document.getElementById(“Status”);

var url = ”http://localhost:8080/REST/database/mark/” + id.value +

”/” + mark.value;

req.onreadystatechange=function() {

if (req.readyState==4 && req.status==200) {

if (req.responseText == “”) {

status.value = “Record of “+id.value+” not exists”;

} else {

status.value = “Record of “+id.value +” updated”;

}

}

}

req.open(“PUT”,url, true);

req.send();

}

</script>

</body>

 

<body>

<input type=”text” id=”ID” size=”10” value=””>

<input type=”text” id=”Mark” size=”5” value=””>

<input type=”text” id=”Status” size=”30” value=””><br/>

 

<button type=”button” onclick=”addRecord()”>Add Record</button>

<button type=”button” onclick=”getRecord()”>Get Record</button>

<button type=”button” onclick=”updateRecord()”>Update Record</button>

 

</body>

</html>

Fig. 1(b)

 

Before any operations

 

After adding the record of 1234567d

 

After updating the mark of 1234567d to 90

 

Retrieving the mark of 1234567d

 

Retrieving/updating the mark of a non-existing record

 

Add a record whose ID already exists

Replaced by your student number

 

 

Fig. 1(c)

 

 

 

 

Q2. Challenging Exercise:[1] Convert Database.java[2] in the SOAP-WSDL Lab to a RESTful service. Write HTML (with Ajax) and Java clients to add author records to the authors table and to query the authors table using the authorID as the key. Here are some example queries and outputs.

 

Add record to the book database

Query the book database in “mysql.exe”

 

Query the book database from a Java client

 

Query the book database from a browser

 

 

Query the book database from a browser

 

[1] Do this exercise if you want to learn more about RESTful web service and challenge yourself.

[2] http://158.132.148.85:8080/download/EIE4108/Database.java