<P>Hi earlier today, I had posted a request for help with some application to Servlet using xml over http scenario.. I am attaching code of the three pieces here, I am facing a java.io.FileNotFoundException and I am lost</P>
<P>Attached is the code and below is the console output of the same along with PrintStackTrace output</P>
<P>*****Application.java *****</P>
<P><pre></P>
<P>package apps;</P>
<P>import java.net.*;<BR>import java.io.*;</P>
<P>import javax.xml.transform.Transformer;<BR>import javax.xml.transform.TransformerConfigurationException;<BR>import javax.xml.transform.TransformerException;<BR>import javax.xml.transform.TransformerFactory;<BR>import javax.xml.transform.stream.StreamResult;<BR>import javax.xml.transform.stream.StreamSource;</P>
<P>import org.jdom.Attribute;<BR>import org.jdom.Comment;<BR>import org.jdom.Document;<BR>import org.jdom.Element;<BR>import org.jdom.JDOMException;<BR>import org.jdom.input.SAXBuilder;<BR>import org.jdom.output.XMLOutputter;</P>
<P><BR> </P>
<P>public class Application {</P>
<P> </P>
<P>public String hostName = "localhost/";<BR>public String webServerStr = "<A href="http://krishna/servlet/MyServlet?xml">http://krishna/servlet/MyServlet?xml</A>=";</P>
<P>String resource="session";<BR>String userid="kris";<BR>String password="kris";</P>
<P><BR>String xml="";</P>
<P><BR>public void init()<BR> {<BR> log("Constructing the xml stream");<BR> String doc;</P>
<P> doc =createDocument();<BR> webServerStr = webServerStr+doc; <FONT color=purple>// this bombs with the FileNotFoundException given below</FONT> , <FONT color=purple>if I do not use this</FONT> <FONT color=purple>no error is thrown but ofcourse the <STRONG>resource </STRONG>would not be found in the Servlet..</FONT></P>
<P> <BR> try<BR> {<BR> // connect to the servlet<BR> System.out.println("Connecting to servlet...");<BR> URL url = new URL( webServerStr );<BR> URLConnection servletConnection = url.openConnection(); <BR> System.out.println("Connected");</P>
<P> // inform the connection that we will send output and accept input<BR> servletConnection.setDoInput(true); <BR> servletConnection.setDoOutput(true);<BR> <BR> // Don't used a cached version of URL connection.<BR> servletConnection.setUseCaches (false);</P>
<P> // Specify the content type that we will send binary data<BR> servletConnection.setRequestProperty<BR> ("Content-Type", "application/octet-stream");<BR> <BR> <BR> // send the student object to the servlet using serialization<BR> sendToServlet(servletConnection, webServerStr);<BR> <BR> // now, let's read the response from the servlet.<BR> // this is simply a confirmation string<BR> readServletResponse(servletConnection); <BR> <BR> <BR> <BR> }<BR> catch (Exception e)<BR> {<BR> System.out.println("ERROR " +e.toString()); <BR> }</P>
<P> }</P>
<P><BR>public static String createDocument() <BR> {<BR> String xmlString="";<BR> <BR> // Create the root element<BR> Element sessionElement = new Element("session");<BR> //create the document<BR> <BR> Document myDocument = new Document(sessionElement);<BR> <BR> <BR> //add a comment<BR> sessionElement.addContent(new Comment("Session Element "));</P>
<P> //add some child elements<BR> /*<BR> * Note that this is the first approach to adding an element and<BR> * textual content. The second approach is commented out.<BR> */<BR> Element resource = new Element("resource");<BR> resource.addContent("session");<BR> sessionElement.addContent(resource);<BR> <BR> //add some more elements<BR> sessionElement.addContent(new Element("useridElement").addContent("kris"));<BR> sessionElement.addContent(new Element("passwordElement").addContent("kris"));<BR> </P>
<P> //print the document</P>
<P> try <BR> {<BR> XMLOutputter outputter = new XMLOutputter(" ", true);<BR> outputter.output(myDocument, System.out);<BR> System.out.println("XMLOUtputter created");<BR> xmlString=outputter.outputString(myDocument);<BR> } <BR> catch (java.io.IOException e) <BR> {<BR> e.printStackTrace();<BR> }<BR> <BR> <BR> //return myDocument;<BR> return xmlString;<BR> }</P>
<P> </P>
<P> <BR>public void sendToServlet(URLConnection servletConnection, String ourRequest)<BR>{<BR> ObjectOutputStream outputToServlet = null;<BR> <BR> try<BR> {<BR> // send the request object to the servlet <BR> log("Sending the request to the servlet...");<BR> outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());<BR> <BR> // serialize the object<BR> //outputToServlet.writeObject(ourRequest);<BR> <BR> outputToServlet.flush(); <BR> outputToServlet.close();<BR> System.out.println("Complete.");<BR> }<BR> catch (IOException e)<BR> {<BR> System.out.println("Bombed in sendToServlet()");<BR> }<BR> </P>
<P>}</P>
<P>protected void log(String msg)<BR> {<BR> System.out.println("Message === " +msg ); <BR> }</P>
<P><BR> /**<BR> * Reads a response from the servlet.<BR> */<BR> public void readServletResponse(URLConnection servletConnection)<BR> {<BR> BufferedReader inFromServlet = null;<BR> <BR> try<BR> {<BR> // now, let's read the response from the servlet.<BR> // this is simply a confirmation string<BR> inFromServlet = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));<BR> <BR> String str;<BR> while (null != ((str = inFromServlet.readLine())))<BR> {<BR> System.out.println("Reading servlet response: " + str);<BR> }<BR> <BR> inFromServlet.close();<BR> }<BR> catch (IOException e)<BR> {<BR> log(e.toString()); <BR> log("readServletResponse problem"); <BR> }<BR> } </P>
<P> public static void main(String[] args) <BR> {<BR> Post post = new Post();<BR> post.init(); <BR> <BR> }<BR>}</P>
<P></pre></P>
<P>** end code Application.java****</P>
<P> </P>
<P>***MyServlet.java****</P>
<P>package myservlets;</P>
<P> </P>
<P>import javax.servlet.*;<BR>import javax.servlet.http.*;<BR>import java.io.*;<BR>import java.util.*;<BR>import app.myapps.util.*;</P>
<P> </P>
<P> </P>
<P>public class MyServlet extends HttpServlet<BR>{</P>
<P><BR>/**<BR>* Parameterless constructor , that invokes the super class constructor<BR>*/<BR> public MyServlet()<BR> {<BR> super();<BR> }</P>
<P><BR> public void service(HttpServletRequest req, HttpServletResponse res)<BR> throws IOException, ServletException<BR> {<BR> <BR> init(req,res);<BR> String xml = req.getParameter("xml");<BR> String value = Parser.getResourceTag(xml);<BR> <BR> System.our.println(" resource is == " +value);<BR> }</P>
<P><BR> public void init(HttpServletRequest req, HttpServletResponse res)<BR> {<BR> addServerInfo(req);<BR> }</P>
<P> private void addServerInfo(HttpServletRequest req)<BR> {<BR> String prot = req.getProtocol();<BR> path = req.getServletPath();<BR> <BR> if (prot.indexOf("/")>0)<BR> {<BR> prot = prot.substring(0,prot.indexOf("/"));<BR> }<BR> path = path.substring(0,path.lastIndexOf("/"));<BR> path = "/app"+path;<BR> System.out.println("PATH = " +path);<BR> <BR> //sinfo=prot+"://"+req.getServerName()+":"+req.getServerPort()+path;<BR> sinfo=prot+"://"+req.getServerName()+":"+req.getServerPort()+path;<BR> svrInfo= prot+"://"+req.getServerName()+":"+req.getServerPort();</P>
<P> System.out.println("SINFO == "+sinfo);<BR> System.out.println("SVRINFO --"+svrInfo);<BR> <BR> }</P>
<P>}<BR></P>
<P>***end Myservlet.java code *******</P>
<P>****** Parser.java</P>
<P>public class Parser {</P>
<P> </P>
<P> public void Parser() <BR> {<BR> }</P>
<P><BR> public static String getResourceTag(String XmlStream)<BR> {<BR> <BR> String Xml;<BR> Xml = parseXml(XmlStream);<BR> //System.out.println("XML IN getResourceTag --->" +Xml);<BR> return Xml;</P>
<P> }</P>
<P> public static String parseXml(String XmlStream)<BR> {<BR> String resource="";<BR> Document doc;<BR> try<BR> {<BR> System.out.println("inside parseXML");<BR> <BR> SAXBuilder sax = new SAXBuilder();<BR> System.out.println("Built the SAXBUilder inside parseXML");<BR> doc = sax.build(new StringReader(XmlStream));</P>
<P><BR> Element root = doc.getRootElement();<BR> <BR> Element r = root.getChild(resource);<BR> <BR> String re = r.toString();<BR> System.out.println("String value of resource in parser -->" +re);<BR> resource = re;</P>
<P> <BR> }<BR> catch(JDOMException e) <BR> {<BR> e.printStackTrace();<BR> }<BR> catch (Exception e)<BR> {<BR> e.printStackTrace();<BR> System.out.println("parsing exception");<BR> }<BR> <BR> <BR> return resource;<BR> }</P>
<P> protected void log(String msg)<BR> {<BR> System.out.println("Message in XMLResourceTag=== " +msg ); <BR>}</P>
<P>} </P>
<P>*******end Parser.java code******</P>
<P> </P>
<P><FONT color=purple>Message === Constructing the xml stream<BR>XMLOUtputter created<BR>After serialize--><?xml version="1.0" encoding="UTF-8"?><BR><session><BR> <!--Session Element --><BR> <resource>session</resource><BR> <useridElement>kris</useridElement><BR> <passwordElement>kris</passwordElement><BR></session></FONT></P>
<P><BR><FONT color=purple>WESERVER STRING --->http://kmuthyal/servlet/MyServlet?xml=<?xml version="1.0" encoding="UTF-8"?><BR><session><BR> <!--Session Element --><BR> <resource>session</resource><BR> <useridElement>kris</useridElement><BR> <passwordElement>kris</passwordElement><BR> </session></FONT></P>
<P><BR><FONT color=purple>Connecting to servlet...<BR>Connected<BR>Message === Sending the request to the servlet...<BR>Complete.<BR>Message === reading response ...<BR>Message === java.io.FileNotFoundException: </FONT><A target=_blank href="http://kmuthyal/servlet/MyServlet??xml=<?xml"><FONT color=purple>http://kmuthyal/servlet/MyServlet??xml=<?xml</FONT></A><FONT color=purple> version="1.0" encoding="UTF-8"?><BR><session><BR> <!--Session Element --><BR> <resource>session</resource><BR> <useridElement>kris</useridElement><BR> <passwordElement>kris</passwordElement><BR> </session><BR> <BR></session><BR>Message === readServletResponse problem<BR>java.io.FileNotFoundException: </FONT><A target=_blank href="http://kmuthyal/servlet/MyServlet??xml=<?xml"><FONT color=purple>http://kmuthyal/servlet/MyServlet??xml=<?xml</FONT></A><FONT color=purple> version="1.0" encoding="UTF-8"?><BR><session><BR> <!--Session Element --><BR> <resource>session</resource><BR> <useridElement>kris</useridElement><BR> <passwordElement>kris</passwordElement><BR> </session><BR></session><BR> at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)<BR> at post.Post.readServletResponse(Post.java:196)<BR> at post.Post.init(Post.java:80)<BR> at post.Post.main(Post.java:217)</FONT></P>
<P><FONT color=purple>Any suggestions?</FONT></P>
<P><FONT color=#800080>Thanks</FONT></P>
<P><FONT color=#800080>Kris</FONT></P><BR><BR><P> </P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P>concern for a cleaner world, for a better tomorrow </P>
<P> <A href="http://www.greenpeace.org">http://www.greenpeace.org</A></P></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE><p><br><hr size=1><b>Do You Yahoo!?</b><br>
<a href="http://personal.mail.yahoo.com/?.refer=mailiyfoot">Yahoo! Mail Personal Address</a> -
Get email at your own domain with Yahoo! Mail.