package apps; import java.net.*; import java.io.*; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.jdom.Attribute; import org.jdom.Comment; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; public class Application { public String hostName = "localhost/"; public String webServerStr = "http://krishna/servlet/MyServlet?xml="; String resource="session"; String userid="kris"; String password="kris"; String xml=""; public void init() { log("Constructing the xml stream"); String doc; doc =createDocument(); webServerStr = webServerStr+doc; try { // connect to the servlet System.out.println("Connecting to servlet..."); URL url = new URL( webServerStr ); URLConnection servletConnection = url.openConnection(); System.out.println("Connected"); // inform the connection that we will send output and accept input servletConnection.setDoInput(true); servletConnection.setDoOutput(true); // Don't used a cached version of URL connection. servletConnection.setUseCaches (false); // Specify the content type that we will send binary data servletConnection.setRequestProperty ("Content-Type", "application/octet-stream"); // send the student object to the servlet using serialization sendToServlet(servletConnection, webServerStr); // now, let's read the response from the servlet. // this is simply a confirmation string readServletResponse(servletConnection); } catch (Exception e) { System.out.println("ERROR " +e.toString()); } } public static String createDocument() { String xmlString=""; // Create the root element Element sessionElement = new Element("session"); //create the document Document myDocument = new Document(sessionElement); //add a comment sessionElement.addContent(new Comment("Session Element ")); //add some child elements /* * Note that this is the first approach to adding an element and * textual content. The second approach is commented out. */ Element resource = new Element("resource"); resource.addContent("session"); sessionElement.addContent(resource); //add some more elements sessionElement.addContent(new Element("useridElement").addContent("kris")); sessionElement.addContent(new Element("passwordElement").addContent("kris")); //print the document try { XMLOutputter outputter = new XMLOutputter(" ", true); outputter.output(myDocument, System.out); System.out.println("XMLOUtputter created"); xmlString=outputter.outputString(myDocument); } catch (java.io.IOException e) { e.printStackTrace(); } //return myDocument; return xmlString; } public void sendToServlet(URLConnection servletConnection, String ourRequest) { ObjectOutputStream outputToServlet = null; try { // send the request object to the servlet log("Sending the request to the servlet..."); outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream()); // serialize the object //outputToServlet.writeObject(ourRequest); outputToServlet.flush(); outputToServlet.close(); System.out.println("Complete."); } catch (IOException e) { System.out.println("Bombed in sendToServlet()"); } } protected void log(String msg) { System.out.println("Message === " +msg ); } /** * Reads a response from the servlet. */ public void readServletResponse(URLConnection servletConnection) { BufferedReader inFromServlet = null; try { // now, let's read the response from the servlet. // this is simply a confirmation string inFromServlet = new BufferedReader(new InputStreamReader(servletConnection.getInputStream())); String str; while (null != ((str = inFromServlet.readLine()))) { System.out.println("Reading servlet response: " + str); } inFromServlet.close(); } catch (IOException e) { log(e.toString()); log("readServletResponse problem"); } } public static void main(String[] args) { Post post = new Post(); post.init(); } }