Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Signed applet talking to server other than its own

 
Vishal Mungi
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below Applet gives java.io.StreamCorruptedException: invalid stream header

//** The Signed Applet on the instance of Tomcat port 8080

public class EchoApplet extends Applet {
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {

// URL urlServlet = new URL(getCodeBase(), "echo");
URL urlServlet = new URL("http", "localhost", 9090,
"/echo");
URLConnection con = urlServlet.openConnection();

con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");

return con;
}

/**
* Send the inputField data to the servlet and show the result in the outputField.
*/
private void onSendData() {
try {
// get input data for sending
String input = inputField.getText();

// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();

// show result
outputField.setText(result);

} catch (Exception ex) {
ex.printStackTrace();
exceptionArea.setText(ex.toString());
}
}
}

//** The Servlet on the other instance of Tomcat port 9090

public class EchoServlet extends HttpServlet {
/**
* Get a String-object from the applet and send it back.
*/
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
System.out.println("Inside Echo Servlet");
response.setContentType("application/x-java-serialized-object");
// read a String-object from applet
// instead of a String-object, you can transmit any object, which
// is known to the servlet and to the applet
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();

// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(echo);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}


************ The same applet calling the servlet in its own container works.
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applet can not talk to any other server than where it was downloaded from.
 
Vishal Mungi
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i have seen examples of applets taking to other servers after being given the permission (net permission) to do so .. like the case of an applet polling various sites & assimilating info. In any case its not giving me a security exception which went off after i signed the applet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic