In my case, the applet was initiating
communication to the servlet. I wasn't acquiring the
connection in the proper order. I have attached a
code sample. Here is the proper order...
1. The applet establishes a URL and Connection to the
servlet.
2. The applet creates an ObjectOutputStream using the
Connection.
3. The applet writes out the object to the
ObjectOutputStream, which is now communicating with
the servlet.
4. Servlet processes data....
5. At this point since a connection is made between
the appelt and servlet, the servlet can send back an
Object, in the provided example I do just that.
6. The applet creates an ObjectInputStream and reads
in the objects sent to it by the servlet.
Applet
======
// servlet & url communication vars
currentPage = getCodeBase();
protocol = currentPage.getProtocol();
host = currentPage.getHost();
port = currentPage.getPort();
try {
// open a connection to the servlet
URL dataURL = new URL(protocol,host,port,"/servlet/com.serv.GetRecipients");
URLConnection connection = dataURL.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
// Don't use a cached version of URL connection.
connection.setUseCaches (false);
connection.setDefaultUseCaches (false);
// Specify the content type that we will send binary data
connection.setRequestProperty ("Content-Type", "application/octet-stream");
// send the servlet our necessary information
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject("testString");
out.flush();
out.close();
// servlet sends back a message
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
status = (
String)in.readObject();
in.close();
}
Servlet
=======
package com.serv;
import java.sql.*;
import com.cc.util.beans.*;
import java.util.*;
import java.io.*;
import com.framework.util.*;
/**
* Insert the type's description here.
* Creation date: (6/5/01 8:09:54 PM)
* @author: Administrator
*/
public class GetRecipients extends javax.servlet.http.HttpServlet {
/**
* Process incoming HTTP GET requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Process incoming HTTP POST requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Returns the servlet info string.
*/
public String getServletInfo() {
return super.getServletInfo();
}
/**
* Initializes the servlet.
*/
public void init() {
// insert code to initialize the servlet here
}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
Boolean retFlag = new Boolean(true);
try
{
ObjectInputStream objIn = new ObjectInputStream(request.getInputStream());
String recipientType = (String)objIn.readObject();
//objIn.close();
//Initialize the return vector
Vector retVec = new Vector();
if(recipientType.equalsIgnoreCase("team"))
{
for(int x = 1; x <= 10; x++)
{
JTeam team = new JTeam();
long id = 10;
id = id+x;
String sName = "Team "+x;
String sDesc = "Description "+x;
String sCreator = "Creator "+x;
String sChngId = "Modified By "+x;
team.setTeamId(id);
team.setTeamName(sName);
team.setTeamDescription(sDesc);
team.setLstModifiedBy(sChngId);
Timestamp ts = Timestamp.valueOf("2001-06-05 00:00:00");
team.setLstMdfyTime(ts);
retVec.addElement(team);
}
}else {
for(int x = 1; x <=10; x++)
{
String sUser = "User "+x;
JUser user = new JUser();
user.setName(sUser);
retVec.addElement(user);
}
}
//set the output attribute
ObjectOutputStream objOut = new ObjectOutputStream(response.getOutputStream());
objOut.writeObject(retVec);
//flush it
objOut.flush();
objOut.close();
}
catch(Throwable theException)
{
// uncomment the following line when unexpected exceptions
// are occuring to aid in debugging the problem.
theException.printStackTrace();
}
}
}