import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
Applet To Servlet Communication
*This applet allows a user to register a new conference member and get and displays a list of
* members.
* The applet communicates w/ a servlet which accesses a database.
* To display the a list of members:
* 1. The applet sends a GET request to the servlet
* 2. The servlet replies w/ a serialized vector of Members
* 3. The results are displayed in the GUI.
*
* To register a new member:
* 1. The user enters the new member’s info thro’ a GUI. A member object is created w/ the input data.
* 2. The applet sends a POST request to the servlet
* 3. The applet sends the serialized student object to the servlet.
* 4. The servlet sends back a confirmation string
*
*/
public class MemberApplet extends Applet
{
private Button showMembers;
private Button registerNewMember;
// All the GUI related widgets are declared here
private String webServerStr = null;
private String hostName = "localhost";
private int port = 8080;
private String servletPath = "/servlet/MemberServlet";
public void init()
{
// All the GUI widgets are created here
// Showing only the important ones
showMembers = new Button("Display Members");
toolBarPanel.add(showMembers);
registerNewMember = new Button("Register new members.");
toolBarPanel.add(registerNewMember);
…
// register listeners
showMembers.addActionListener(new ShowMembersActionListener());
registerNewMember.addActionListener(new RegisterNewMemberActionListener());
// get the host name and port of the applet's web server
URL hostURL = getCodeBase();
hostName = hostURL.getHost();
port = hostURL.getPort();
if (port == -1)
{
port = 80;
}
log("Web Server host name: " + hostName);
webServerStr = " + hostName + ":" + port + servletPath;
log("Web String full = " + webServerStr);
}
/**
* Get a list of members by a GET request to the servlet.
* The servletwill return a vector of Member objects.
*
*/
public Vector getMemberList()
{
ObjectInputStream inputFromServlet = null;
Vector members = null;
try
{
// build a GET url string w/ encoded name/value pairs
String servletGET = webServerStr + "?"
+ URLEncoder.encode("UserOption") + "="
+ URLEncoder.encode("AppletDisplay");
// connect to the servlet
URL memberservlet = new URL( servletGET );
URLConnection servletConnection = memberServlet.openConnection();
// Read the input from the servlet.
//The servlet will return a serialized vector containing Members.
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
members = readMembers(inputFromServlet);
}
catch (Exception e){}
return members;
}
/**
* Read the input from the servlet. *
*/
protected Vector readMembers(ObjectInputStream theInputFromServlet)
{
Vector theMembers = null;
try{
// read the serialized vector of member objects fromthe servlet
theMembers = (Vector) theInputFromServlet.readObject();
theInputFromServlet.close();
}
catch (Exception e){}
return theMembers;
}
public void registerMember()
{
PrintWriter outTest = null;
BufferedReader inTest = null;
Member theMember = null;
try
{
// connect to the servlet
URL memberServlet = new URL( webServerStr );
URLConnection servletConnection = memberServlet.openConnection();
// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
// Don't used a cached version of URL connection.
servletConnection.setUseCaches (false);
// Specify the content type that we will send binary data
servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
// send the member object to the servlet using serialization
sendMemberToServlet(servletConnection, theMember);
// read the response (a confirmation) from the servlet.
readServletResponse(servletConnection);
}
catch (Exception e){}
}
// Listeners as inner classes
class ShowMembersActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
// Get a list of the members
Vector memberVector = getMembers();
// Display the list
displayMembers(members);
}
catch (Exception e)
{
System.out.println("Error: " + e);
}
}
}
class registerButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
registerMember();
}
}
}
private void readServletResponse(URLConnection servletConnection)
{
BufferedReader inFromServlet = null;
try{
// now, read the confirmation from the servlet.
inFromServlet = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));
String str;
while (null != ((str = inFromServlet.readLine())))
{
//System.out.println("Reading servlet response: " + str);
}
inFromServlet.close();
}
catch (IOException e){}
}
/**
* Sends a Member object to a servlet.
*The Member object is serialized over the URL connection
*/
private void sendMemberToServlet(URLConnection servletConnection, Member theMember)
{
ObjectOutputStream outputToServlet = null;
try
{
// send the Member object to the servlet using serialization
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
// serialize the object
outputToServlet.writeObject(theMember);
outputToServlet.flush();
outputToServlet.close();
log("Complete.");
}
catch (IOException e)
{
log(e.toString());
}
}
}
//------File:Member.java ------
public class Member implements java.io.Serializable
{
// data members
private String lastName;
private String firstName;
…
}