Scott Williams

Greenhorn
+ Follow
since Jun 23, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Scott Williams

For the FileNotFound exception, make sure that the file or servlet you are trying to access with the ObjectInputStream is in the right location. Most servlet runners want the servlets in a particular location.
23 years ago
The conversation can be started from any method. I am doing it in an actionPerformed() method. This method is being called when a button is being pressed within the applet.
23 years ago
Sorry, I mean <jsp:plugin> without the smileys...
Scott
23 years ago
The conversation begins at the above line:
URL dataURL = new URL(protocol,host,port,"/servlet/com.serv.GetRecipients");
The applet gets a Connection to the servlet then an ObjectOutputStream is created, this is where the Object is written to.
Please let me know if there are any more questions.

This works with both IE 5.5 and Netscape 4.7. Depending on which app server you are using the <jsp lugin> tag can be used, however it does not work on WebSphere 3.5. I recommend using the HTML Converter if the <jsp lugin> tag does not work for you.
23 years ago
I wasn't able to pass a Vector from JSP to an applet because JSP automatically sets the output to a JSPWriter. In this case the output needs to be binary since an object is being written out (Vector). So, to get around this I had to use a servlet. The way I did it was to have the
applet initiate a conversation to a servlet and ask
the servlet for the Vector. Please see the below example code. Here are the steps:
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();
}
}
}
23 years ago
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();
}
}
}
23 years ago
Thanks for your help. I put your code in my JSP and now get IllegalStateException on the new ObjectOutputStream line. Any ideas???
23 years ago
I think the problem here is that the default output stream for JSP is of type JSPWriter and I am trying to change it to ObjectOutputStream. What do you guys think? Any experience with this??
Scott
23 years ago
Thanks for your replies. I am encountering this exception (java.lang.IllegalStateException) now when the following line of code is being executed in my JSP.
ObjectOutputStream outputToApplet = new ObjectOutputStream(response.getOutputStream());
Any ideas???
Thank you.
23 years ago
URLConnection to the servlet before the servlet does a
writeObject(Object)
? I'm doing a
writeObject(Object), flush() and close()
in a servlet and then establishing a connection to the servlet from the applet and when the
getInputStream()
is invoked in the applet I get a FileNotFoundException thrown and the array is displayed in binary in the HTML. Thanks in advance
23 years ago
Is it possible to pass a Vector from JSP to an applet? If so how? Thanks in advance.
Scott
23 years ago
Is it possible to pass a Vector from JSP to an applet? If so how? Thanks in advance.
Scott
23 years ago