Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

receiving serialized object from servlet to applet

 
su ferrente
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to retrieve an object from servlet to applet. When I try to run my applet I get this exception:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2165)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2634)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:734)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
at cmsapplet.applet.DisplayFileExplorerApplet.getIFileComposite(Unknown Source)
at cmsapplet.applet.DisplayFileExplorerApplet.init(Unknown Source)
at sun.applet.AppletPanel.run(AppletPanel.java:348)
at java.lang.Thread.run(Thread.java:536)

I have no idea what is causing this exception. I have searched the net for some clues but didn't find anything that makes sense.

Here is my code for applet:
public class DisplayFileExplorerApplet extends JApplet{

IFileComposite m_composite;

/**
* This is invoked when the applet is called.
*/
public void init(){

try {
Container contentPane = this.getContentPane();
m_composite = this.getIFileComposite();

FileExplorer fexplorer = new FileExplorer(m_composite);
contentPane.add(fexplorer);

}
catch(MalformedURLException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}// end of init()

/**
* This method gets a HttpURLConnection.
* @return HttpURLConnection: the connection.
* @throws MalformedURLException
* @throws IOException
*/

private HttpURLConnection getServletConnection() throws MalformedURLException,
IOException{

String host = "Http://localhost:8020/cms/FileCompositeServlet";
URL urlServlet = new URL(host);
HttpURLConnection con = (HttpURLConnection)urlServlet.openConnection();

con.setDoInput(true);

con.setDoOutput(true);

con.setUseCaches(false);

con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-java-serialized-object");


return con;
}

/**
* Returns an IFileComposite object that was retrieved from the servlet.
* @return IFileComposite: the file composite containing all the relevent information
* @throws IOException
*/

private IFileComposite getIFileComposite() throws IOException{
System.err.println("inside getIFileComposite method in DisplayFileExplorerApplet");

try{
URLConnection con = getServletConnection();

con.connect();
InputStream instrm = con.getInputStream();

ObjectInputStream inputFromServlet = new ObjectInputStream(instrm);

IFileComposite result = (IFileComposite) inputFromServlet.readObject();
inputFromServlet.close();
instrm.close();

return result;
}
catch(ClassNotFoundException e){
System.err.println("class not found exception.");
e.printStackTrace();
}

return null;

}

}// end of FileExplorerApplet

And the code for servlet:
public class FileCompositeServlet extends HttpServlet{
private IFileComposite m_composite;
private FileService fService;
private final String root = "f:/MSc_project/example_website";

/**
* This method received http request from the DisplayFileExplorer applet
* and creates an IFileComposite object for a specified home directory.
* It then sends the IFileComposite object back to the calling applet.
*
* @param request: the request
* @param response: the response
*
* @throws ServletException
* @throws IOException
*/
public void doPost(
HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {

try{
response.setContentType("application/x-java-serialized-object");

String root = new String("f:/example-website");
// now call FileService.getFileHierarchy method
boolean isRoot = true;
fService = FileService.getInstance();
m_composite = fService.getFileHierachy(root, isRoot);

// return the composite object back to calling applet
OutputStream outputStream = response.getOutputStream();
ObjectOutputStream objOutStr = new ObjectOutputStream(outputStream);
objOutStr.writeObject(m_composite);
objOutStr.flush();
objOutStr.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}

Can someone point me to the right direction please???

Thanks

Su
 
su ferrente
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I manage to sort out the problem. It was my FileService class that the servlet was calling giving NullPointerException because I was trying to get the length of my ArrayList which could be null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic