File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Http Tunneling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Http Tunneling" Watch "Http Tunneling" New topic

Http Tunneling

Sid Scud
Ranch Hand

Joined: Mar 13, 2001
Posts: 32
I am trying to connect to a servlet from an applet using URLConnection.
This the code in the applet :
url = new URL("");
con = url.openConnection();
con.setRequestProperty("Content-TYpe", "application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream()) ;
oos.writeObject("TEst data") ;
oos.flush() ;
oos.close() ;
ObjectInputStream ois = new ObjectInputStream(con.getInputStream()) ;
catch(Exception e){}
This is my service method :
public void service( HttpServletRequest req, HttpServletResponse res ) throws IOException {
res.setContentType( "text/html" );
PrintWriter out = res.getWriter();
out.println( "<CENTER><FONT SIZE=+1 FACE=\"arial\">Servlet Ran Successfully! POST</FONT></CENTER>" );
ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
out.println("writing the object");
oos.writeObject("test data");
catch(Exception e){
out.println("Exception "+e);
But I keep on getting StreamCorrupted exception in: invalid stream header error in Jrun and http response code 405 IO exception in Tomcat...
can someone help???
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Your service method is writing text, but your applet is trying to read a serialized object. You cant just arbitrarily write both text and an object to out.
Change your applet to read a character stream from con.getInputStream.
Also, you probably should just flush and not close the applet output stream. Close it later when everything is finished.
[ April 11, 2002: Message edited by: William Brogden ]
Eric Peterson
Ranch Hand

Joined: Jan 08, 2002
Posts: 31
There are several things I might suggest here. First I do not believe it is wise to override the service method for your servlet you should just override doGet/doPost. That is not really the point of your question though, is it. First I believe without looking at the javadoc for the URL class the best thing to do is to set the response type in your servlet and not necessarily on the applet side. On the servlet side you cannot mix the response types.
This is my service method :
public void service( HttpServletRequest req, HttpServletResponse res ) throws IOException {
res.setContentType( "text/html" );

You should set the response type to something like this
Here are couple of links that might be helpful.
A chapter from Marty Hall's book on http tunneling. And a question of the week from Sun's developer site on communicating with servlets. I hope this will help you out.

Eric Peterson
I agree. Here's the link:
subject: Http Tunneling
It's not a secret anymore!