File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet to Applet communication

 
Scott Williams
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Charlie Boss
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please post that particular code and error
 
Scott Williams
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
Rakesh Ray
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give some more info?
1. From where do you initiate Applet to Servlet communication.
e.g. Do you call applet method from applet(java) or from javascript.
2. Is the behaviour different when tried with Netscape and IE? (Versions?).
 
Scott Williams
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Williams
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I mean <jsp:plugin> without the smileys...
Scott
 
Rakesh Ray
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you initiating communication to servlet within init() method
of the applet or it is saperate method being invoked from outside the applet.??
There is a potential problem when you do call applet method from
outside the applet( HTML/JAVASCRIPT) and that method is tring to connect to open connection..
Another confusion from my side is that why it should throw file not found instead of any IO exception while doing getInputStream() from the Applet.
 
Scott Williams
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Williams
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
venkat thota
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi williams,
I hope ur problem is while creating the URL itself.
URL dataURL = new URL(protocol,host,port,"/servlet/com.serv.GetRecipients");
print this dataURL and check.
venkat.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic