This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes EOF error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "EOF error" Watch "EOF error" New topic
Author

EOF error

Gurbhej Singh Dhillon
Greenhorn

Joined: Oct 04, 2007
Posts: 10
hello guys, i am writing applet-servlet communication application, i am getting this error, which i am unable to resolve after digging into google also, hope someone here could help me get rid of the error. thanks in advance

code at applet

....
URL url = new URL(getCodeBase(), "NewServlet");
URLConnection con = url.openConnection();

con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(false);


ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(stream);
oos.writeObject(obj);
oos.flush();
oos.close();

byte[] buf = stream.toByteArray();
// con.setRequestProperty ("Content-Type", "application/x-java-serialized-object");
con.setRequestProperty("CONTENT-LENGTH",Integer.toString(buf.length));


DataOutputStream dataout = new DataOutputStream(con.getOutputStream());
dataout.write(buf);
dataout.flush();
dataout.close();
....

code at servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
....
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
InputStream in = request.getInputStream();
try
{
ObjectInputStream ois = new ObjectInputStream(in);
}
catch(EOFException e1)
{
System.out.println(" EXCEPTION ");
e1.printStackTrace();
}
....
}




java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2748)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at NewServlet.doPost(NewServlet.java:112)
at NewServlet.doGet(NewServlet.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
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:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)


also another thing :
when i do System.out.println(" Content Length = " + request.getContentLength()); it shows -1

and at applet side when i write
JOptionPane.showMessageDialog(this,"Priting buf length \n" + buf.length,"Testing!!" , JOptionPane.WARNING_MESSAGE); it shows 501
while
JOptionPane.showMessageDialog(this,"Priting con length \n" + con.getContentLength(),"testing!!" , JOptionPane.WARNING_MESSAGE); shows 234
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
You are attempting to connect to a servlet without using HTTP. Instead of URLConnection use HttpUrlConnection.
Gurbhej Singh Dhillon
Greenhorn

Joined: Oct 04, 2007
Posts: 10
i changed it to
HttpURLConnection con = (HttpURLConnection)url.openConnection();
but still the error persists
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Same error?
Gurbhej Singh Dhillon
Greenhorn

Joined: Oct 04, 2007
Posts: 10
yes, same error !!
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
I am not sure, but why are you writing the object to a ByteArrayOutputStream before streaming it across thew wire?

Just get a handle to the stream from the HttpUrlConnection and create your object stream with this. Then write your object to this stream.

I thin this is the nature of your problem.
Gurbhej Singh Dhillon
Greenhorn

Joined: Oct 04, 2007
Posts: 10
yes i was using that before only, but was getting same here, untill someone suggested to use ByteArrayOutputStream + DataOutputStream to set the content length, which might be the reason for EOF Exception. both ways none is working
Gurbhej Singh Dhillon
Greenhorn

Joined: Oct 04, 2007
Posts: 10
does any1 has an answer to my question ??.. thought newbies questions would be answered over here, alas
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Originally posted by Gurbhej Singh Dhillon:
thought newbies questions would be answered over here, alas
Perhaps you should ask for a refund?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
What you are coding is not a daily thing for most people, unlike most questions/problems that are posted.
In order to help you a person would either have to receate your problem and debug or search the net for an example that works and compare it to your problem.
This is a lot of work to ask other people to do on your behalf...
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Here is an example I found on the net for sending an object across the wire. Please compare it to your code.




and the code the de-serialize the object on the server side

Val�ry Urbain
Greenhorn

Joined: Oct 21, 2007
Posts: 11
Originally posted by Michael Ku:
Here is an example I found on the net for sending an object across the wire. Please compare it to your code.



Excellent!
Not that I want to be picky, but you should have pointed out from where you have got this code..
Laurentiu Bold
Greenhorn

Joined: Oct 15, 2007
Posts: 11
Michael,

Don't jump to the conclusion . Gurbhej will probabily use your example and if it's what he needs, he will thank you if you want it... but "picky" was Val�ry Urbain who might be (or not) the author of your found source code.

Thank you
[ October 22, 2007: Message edited by: Laurentiu Bold ]
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Laurentiu, thank you for pointing out my mistake. I have deleted my post.

 
jQuery in Action, 2nd edition
 
subject: EOF error
 
Similar Threads
java.net.SocketTimeoutException: Read timed out
Urgent!!! Applet and Servlet problems!
unexpected exception:please help
Java socket EOFException() Windows
Applet-JSP-JAVABean ERROR