0

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.

Abdul Khaliq

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Abdul Khaliq
  • 2,423
  • 12
  • 40
  • 65
  • do applet has to do anything with certificates to applet signing – Abdul Khaliq Jan 12 '11 at 08:23
  • unsigned applet can only work with same domain. – J-16 SDiZ Jan 12 '11 at 08:46
  • i have a separate jar file for applet and may tomcat application is a different project does the different code base is an issue ? The applet is self signed – Abdul Khaliq Jan 12 '11 at 10:55
  • 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 – Abdul Khaliq Jan 12 '11 at 15:59
  • You need to call and consume `getInputStream()` to actually fire the request. See also http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – BalusC Jan 22 '11 at 14:15

1 Answers1

2

the server side code does not get called until we read bytes from the opened stream

Abdul Khaliq
  • 2,423
  • 12
  • 40
  • 65