This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes Applet to Servlet Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet to Servlet Communication" Watch "Applet to Servlet Communication" New topic
Author

Applet to Servlet Communication

S.R.K.Vivek Raju
Ranch Hand

Joined: Sep 23, 2004
Posts: 58
Hi All,

I am actually calling a servlet from an applet to generate a PDF report.

I have to pass certain objects from applet to servlet.
The code on my applet is as follows

Vector vec = new Vector();
vec.add("abc");
vec.add("xyz");
String issueReportUrl="http://localhost/sr/servlet/TBQReports";//getTBQReportPath();
StringBuffer sb = new StringBuffer(issueReportUrl);
try {
URL url = getURL(sb.toString());
URLConnection servletConnection = url.openConnection();
if (servletConnection instanceof HttpURLConnection)
{
((HttpURLConnection)servletConnection).setRequestMethod("POST");
}
else
{
System.out.println("this connection is NOT an HttpUrlConnection connection");
return;
}
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
OutputStream os= servletConnection.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
out.writeObject(vec);
out.flush();
out.close();
parentModule.getAppletContext().showDocument(url, "_blank");
} catch (Exception tr) {
Utility.showMsgBox(Utility.findParentFrame(this), "In actionPerformed" + tr.getMessage(), 1)
}


The code in my servlet is as follows

ObjectInputStream inputFromApplet = null;
PrintWriter out = null;
Vector data = null;
try {
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(new BufferedInputStream(request.getInputStream()));
System.out.println("read Object...");
// read the serialized data from applet
Vector o = (Vector)inputFromApplet.readObject();
if(o != null && (o instanceof Vector))
{
data = (Vector) o;
System.out.println("Got Object"+data.get(0));
inputFromApplet.close();

// send back a confirmation message to the applet
out = new PrintWriter(response.getOutputStream());
response.setContentType("text/plain");
out.println("confirmed " + data.get(0));
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}

When i run the code, i get the following exception

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream
.java(Compiled Code))
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputS
tream.java:2671)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:767
)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:286)
at TBQReports.doPost(TBQReports.java:70)
at TBQReports.doGet(TBQReports.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServl
et.java:417)
at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java
:131)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:198)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:144)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:138)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)

at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:
2459)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:132)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatche
rValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:593)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:126)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
t.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)

at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:15
2)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
rocessConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
lowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:568)

The line number that it is pointing is
inputFromApplet = new ObjectInputStream(new BufferedInputStream(request.getInputStream())); in my servlet.

I tried flush the data, closing the outputStream. But still couldnt figure out its cause.

Please help.

PS:- The same code works if i pass the values as query string.But the data is large hence i want to pass it as an Object.

Thanks,
Regards,
S.R.K.Vivek Raju
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Are applet and servlet running on the same version of the JDK? Serialization is known to break between JDK releases.

As an aside, please UseCodeTags when posting code of any length to keep the formatting intact. It's very hard to comprehend as it is.


Ping & DNS - my free Android networking tools app
S.R.K.Vivek Raju
Ranch Hand

Joined: Sep 23, 2004
Posts: 58
Hi Ulf,

Yeah sure, i would use the code tags from now onwards.
And yes, the Servlet and Applet are running on the same version of JDK.


Thanks,
Regards,
S.R.K.Vivek Raju.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You could try not using a BufferedInputStream. In other words, just "new ObjectInputStream(request.getInputStream())".
 
 
subject: Applet to Servlet Communication