Forum Stats

  • 3,836,894 Users
  • 2,262,203 Discussions
  • 7,900,136 Comments

Discussions

calling servlet from applet?

830552
830552 Member Posts: 3
edited Jan 12, 2011 11:01AM in Java Programming
i am trying to call servlet from applet below is the calling code

ObjectOutputStream outputToServlet = null;
try {
//String xmlToSign = this.getParameter("xmltosign");
String xmlToSign ="<?xml version=\"1.0\" encoding=\"UTF-8\"?> <root> <name> hello world</name></root> ";
URL signServlet = new URL("http://localhost:8084/SignXMLDemo/mtservlet");
URLConnection servletConnection = signServlet.openConnection();
servletConnection.setDoInput(false);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
String encodedValue = new BASE64Encoder().encode(xmlToSign.getBytes());
outputToServlet.writeObject(encodedValue);
outputToServlet.flush();
outputToServlet.close();
JOptionPane.showMessageDialog(this, "XML successfully signed and sent to server.");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
} finally {
try {
outputToServlet.close();
} catch (IOException ex) {
Logger.getLogger(SignApplet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
the issue with the code is that servlet in not being called can any one help in this what i am missing in the code. The URL is correct as it can be called from browser i am using ie 9 windows 7 machine. How can i debug this situation. The applet is signed and i am integrating this applet in a tomcat 6 we application in Netbeans.

Abdul Khaliq

Edited by: user13709878 on Jan 12, 2011 2:37 AM
Tagged:

Answers

  • sabre150
    sabre150 Member Posts: 1,405
    1) I would have expected you to use the POST method.
    2) I don't see why you are using ObjectOutputStream; you could just sent the utf-8 encoded bytes of the XML.
    3) I don't see why you are Base64 encoding; again, you could just sent the utf-8 encoded bytes of the XML.
  • 830552
    830552 Member Posts: 3
    even i changed the code to following

    URL signServlet = new URL("http://192.168.3.90:8084/SignXMLDemo/signservlet");
    HttpURLConnection servletConnection = (HttpURLConnection) signServlet.openConnection();
    servletConnection.setRequestMethod( "POST" );
    servletConnection.setDoOutput(true); // to allow us to write to the URL
    servletConnection.setUseCaches(false); // Write the message to the servlet and not from the browser's cache
    // simple output stream
    outputToServlet =servletConnection.getOutputStream();

    outputToServlet.write(xmlToSign.getBytes());
    outputToServlet.flush();
    outputToServlet.close();
    JOptionPane.showMessageDialog(this, "XML successfully signed and sent to server.");

    there seems no hit to the server at all but the code executes successfully i get the the message written at the last line
  • 830552
    830552 Member Posts: 3
    edited Jan 12, 2011 11:01AM
    I tested the applet using simple java sockets and it works fine but i cannot understand why it is not making the connection the webserver even though the URL is correct and the every line of code is executing successfully

    Edited by: user13709878 on Jan 12, 2011 8:00 AM
This discussion has been closed.