Chapter 11
Chapter 11
Chapter 11
Chapter 11
Chapter 11
Internet Applications- Part 2
Lecture Suggestions
Unless you desire otherwise, focus your students’ attention to the protocols and
APIs introduced rather than to the development of aesthetic web pages.
Students are expected to be able to use HTML to compose basic web pages, but
should be discouraged from spending undue effort in the presentation of web
pages.
The common thread of the chapter is the HTTP: all the toolkits and protocols
introduced in the chapter are built on top of HTTP.
Inevitably, you will have some students in your class who have had experience
with Internet programming using Perl, .net, etc. Explain to them that all these
toolkits have correlation with servlets and encourage them to identify the
correlations as they are introduced to servlets.
Emphasize the availability of session objects as a server-side session state data
repository, as opposed to cookies, which reside on the client-side.
Encourage your students to write client programs to access as many free SOAP
services as possible (see reference 16.)
In presenting servlets, run each code samples in-class after going over the code.
In presenting SOAP services:
o run a client program in-class to access a SOAP service as possible on
xmethod.com (see reference 16.)
o create a simple SOAP service in class and go through the steps of how to
deploy it
o explain that there are two styles of SOAP requests and responses. In the
book, the Remote Procedure Call (RPC) style is presented. An alternate
style encodes the requests and responses as Web documents, so that they
are interpreted just like other web contents.
This chapter continues where Chapter 9 left off. It introduces the students to Java-
based Internet software development tools: Java applets and Java Servlets, and to
web services by way of the Simple Object Access Protocol (SOAP).
Servlets Exercises
2.
c.
Compile the Counter1.java file, and install the resulting class file Counter1.class to the
<servlet class file directory>. Then browse to it using the <servlet URL path>. Refresh
the browser repeated to run the Counter1 servlet several times. Describe the counter value
displayed by the browser successively.
The value starts at 1 and is incremented by 1 with each successive refresh of
the page (which causes the servlet to be re-executed).
d.
Open another browser and browse to the Counter1 servlet. Describe the counter value
displayed by the browser.
The increment of the counter value continues. The same servlet is executed
for both browser sessions. As a result, the same counter value is shared.
e.
Close the browser windows. Then reopen a browser window and browse to the Counter1
servlet. Describe the counter value displayed by the browser.
The counter value continues. The same servlet is executed for the new
browser session. The servlet persists once it is initiated, even when no active
browser session calls for it.
f.
h.
Modify Counter1.java so that the counter value is incremented by 2 each time.
Recompile and refile the class file. NOTE: Because a servlet is persistent, you must
shutdown the server and restart it before the new servlet will take effect. Show the
code change.
j.
Compile GetForm.java and PostForm.java. and install the resulting class files to the
<servlet class file directory>.
Making sure that your servlet engine is active, browse the page GetForm.html then
PostForm.html. Did the servlets run? Compare the outputs – including the URL
displayed in the browser -- with those generated using CGI scripts in the previous
lab.
The outputs are similar. With the GetForm, the URL displayed in the browser
looks like:
http://localhost:8080/examples/servlet/GetForm?
name=john&quest=happy&color=chartreuse&swallow=african&text=Live+o
r+die%21
and the name-value pairs in the query string are displayed in the browser window.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
name = req.getParameter("name").trim( );
password = req.getParameter("password").trim( );
}
} //end class
SOAP Exercises
4. Write a SOAP service (interface and class) which provides two methods: (i) add, which
accepts two integers and return the sum, and (ii) subtract,which accepts two integers
and return the difference. Deploy it. Write a client program which invokes the two
methods and process the outcomes.
The README file
CalService -- a SOAP service sample
The rest of the steps are to be run from the folder created above:
2. Run deploy.bat, which contains this command:
java org.apache.soap.server.ServiceManagerClient
http://localhost:8080/soap/servlet/rpcrouter
deploy DeploymentDescriptor.xml
3. Run list.bat, which contains this command:
java org.apache.soap.server.ServiceManagerClient
http://localhost:8080/soap/servlet/rpcrouter list
You should see the service name: urn:onjavaserver, among
The service:
package onjava;
public class CalcService {
public int add(int p1, int p2) {
return p1 + p2;
}
return p1 - p2;
}
} //end class
The client:
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
// make the call: note that the action URI is empty because the
// XML-SOAP rpc router does not need this. This may change in the
// future.
Response resp = call.invoke(url, "" );
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:onjavaserver">
<isd:provider type="java"
scope="Application"
methods="add subtract">
<isd:java class="onjava.CalcService"/>
</isd:provider>
<isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener>
</isd:service>