applet to servlet EOFException
Hi,
I'm trying to send an object to a servlet for processing. However, I keep getting an EOFException or NullPointerException (depending on the order I arrange my code in the applet) on the readObject() method on the servlet. I've checked my code against numerous examples on the NET and don't see an obvious difference. Can someone please help.
Please advise,
Alan Shiers
*******************************************
TOMCAT LOG FILE:
Attempting to read Object...
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at TableDataServlet.doPost(TableDataServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFil terChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain .java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java: 214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:10 7)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnecti on(Http11Protocol.java:731)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:5 26)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorke rThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:6 84)
at java.lang.Thread.run(Unknown Source)
TABLEDATASERVLET:
Code:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TableDataServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
ObjectInputStream inputFromApplet = null;
TableData td = null;
PrintWriter out = null;
String[][] data = null;
try
{
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(new BufferedInputStream(request.getInputStream()));
System.out.println("Attempting to read Object...");
// read the serialized data from applet
Object o = inputFromApplet.readObject(); //<<This is the culprit!!
if(o != null && (o instanceof TableData))
{
td = (TableData) o;
System.out.println("Got Object");
inputFromApplet.close();
data = td.getData();
String firstname = data[0][1];
// send back a confirmation message to the applet
out = new PrintWriter(response.getOutputStream());
response.setContentType("text/plain");
out.println("confirmed " + firstname);
out.flush();
out.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
class TableData implements Serializable
{
String[][] data = null;
public TableData(String[][] newData)
{
if(newData != null)
data = newData;
}
public String[][] getData()
{
return data;
}
public void setData(String[][] newData)
{
if(newData != null)
data = newData;
}
}
}
TABLEFORM APPLET:
Code:
package forms;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import java.applet.*;
import java.io.*;
import java.net.*;
public class TableForm extends JApplet implements ActionListener
{
...
public void actionPerformed(ActionEvent e)
{
...
if(e.getSource() == submit)
{
...
//Load up a 2D array
String[][] data = new String[RowCount][headers.length];
for(int i = 0; i < RowCount; i++)//rows
{
for(int j = 1; j < headers.length; j++)//columns
{
data[i][j - 1] = (String)dm.getValueAt(i,j);
}
}
TableData td = new TableData(data);
try
{
// connect to the servlet
String location = processingServlet;
URL testServlet = new URL( location );
URLConnection servletConnection = testServlet.openConnection();
if (servletConnection instanceof HttpURLConnection)
{
((HttpURLConnection)servletConnection).setRequestMethod("POST");
}
else
{
System.out.println("this connection is NOT an HttpUrlConnection connection");
return;
}
//inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
//Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
//Specify the content type that we will send binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");
//send your data to the servlet
statusTextArea.append("Sending data to servlet..." + "\n");
ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));
outputToServlet.writeObject(td);
outputToServlet.flush();
outputToServlet.close();
statusTextArea.append("Data has been sent." + "\n");
//now wait for a response from the servlet
statusTextArea.append("Waiting for response..." + "\n");
String line = "";
while((line = in.readLine()) != null)
{
statusTextArea.append(line + "\n");
}
in.close();
statusTextArea.append("Communication ended." + "\n");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
private boolean isNumeric(String str)
{
for(int i = 0; i < str.length(); i++)
{
if(!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
class TableData implements Serializable
{
String[][] data = null;
public TableData(String[][] newData)
{
if(newData != null)
data = newData;
}
public String[][] getData()
{
return data;
}
public void setData(String[][] newData)
{
if(newData != null)
data = newData;
}
}
}
applet to servlet EOFException
My understanding is that it isn't necessary to implement the two methods you mentioned except in special cases. The class is just a wrapper for a String array. That's not very complicated. If you think those two methods are really necessary, how would you implement them?
Alan