File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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 :
try{
url = new URL("http://128.9.200.131:8080/sid/servlet/ClientServlet");
con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
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()) ;
textArea1.append(ois.readObject()+"\n");
ois.close();
}
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>" );
try{
ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
out.println(ois.readObject());
ois.close();
ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
out.println("writing the object");
oos.writeObject("test data");
oos.flush();
oos.close();
}
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
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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.
Bill
[ April 11, 2002: Message edited by: William Brogden ]
Eric Peterson
Ranch Hand

Joined: Jan 08, 2002
Posts: 31
Siddarth,
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
or
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: http://aspose.com/file-tools
 
subject: Http Tunneling