This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing a Vector from JSP to an applet

 
Scott Williams
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to pass a Vector from JSP to an applet? If so how? Thanks in advance.
Scott
 
claude favre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do it that way.
On the JSP side:
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(yourVector);
oos.close()
on the Applet side:
public getVector()
{
Vector aVEctor = new Vector();
try
{
URL url = new URL(Url of above JSP page);
ObjectInputStream ois = new ObjectInputStream(url.openStream());
aVEctor = (Vector)ois.readObject();
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
Hope this help.
Claude
 
Scott Williams
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I put your code in my JSP and now get IllegalStateException on the new ObjectOutputStream line. Any ideas???
 
Scott Williams
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic