Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 442 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
calling servlet from applet?

830552
Member Posts: 3
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
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
-
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. -
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 -
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.