[jdom-interest] help !!!!!
krishna muthyala
muthyalakris at yahoo.com
Mon Jun 4 15:27:04 PDT 2001
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
Attached is the code and below is the console output of the same along with PrintStackTrace output
*****Application.java *****
<pre>
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; // this bombs with the FileNotFoundException given below , if I do not use this no error is thrown but ofcourse the resource would not be found in the Servlet..
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();
}
}
</pre>
** end code Application.java****
***MyServlet.java****
package myservlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import app.myapps.util.*;
public class MyServlet extends HttpServlet
{
/**
* Parameterless constructor , that invokes the super class constructor
*/
public MyServlet()
{
super();
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
init(req,res);
String xml = req.getParameter("xml");
String value = Parser.getResourceTag(xml);
System.our.println(" resource is == " +value);
}
public void init(HttpServletRequest req, HttpServletResponse res)
{
addServerInfo(req);
}
private void addServerInfo(HttpServletRequest req)
{
String prot = req.getProtocol();
path = req.getServletPath();
if (prot.indexOf("/")>0)
{
prot = prot.substring(0,prot.indexOf("/"));
}
path = path.substring(0,path.lastIndexOf("/"));
path = "/app"+path;
System.out.println("PATH = " +path);
//sinfo=prot+"://"+req.getServerName()+":"+req.getServerPort()+path;
sinfo=prot+"://"+req.getServerName()+":"+req.getServerPort()+path;
svrInfo= prot+"://"+req.getServerName()+":"+req.getServerPort();
System.out.println("SINFO == "+sinfo);
System.out.println("SVRINFO --"+svrInfo);
}
}
***end Myservlet.java code *******
****** Parser.java
public class Parser {
public void Parser()
{
}
public static String getResourceTag(String XmlStream)
{
String Xml;
Xml = parseXml(XmlStream);
//System.out.println("XML IN getResourceTag --->" +Xml);
return Xml;
}
public static String parseXml(String XmlStream)
{
String resource="";
Document doc;
try
{
System.out.println("inside parseXML");
SAXBuilder sax = new SAXBuilder();
System.out.println("Built the SAXBUilder inside parseXML");
doc = sax.build(new StringReader(XmlStream));
Element root = doc.getRootElement();
Element r = root.getChild(resource);
String re = r.toString();
System.out.println("String value of resource in parser -->" +re);
resource = re;
}
catch(JDOMException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("parsing exception");
}
return resource;
}
protected void log(String msg)
{
System.out.println("Message in XMLResourceTag=== " +msg );
}
}
*******end Parser.java code******
Message === Constructing the xml stream
XMLOUtputter created
After serialize--><?xml version="1.0" encoding="UTF-8"?>
<session>
<!--Session Element -->
<resource>session</resource>
<useridElement>kris</useridElement>
<passwordElement>kris</passwordElement>
</session>
WESERVER STRING --->http://kmuthyal/servlet/MyServlet?xml=<?xml version="1.0" encoding="UTF-8"?>
<session>
<!--Session Element -->
<resource>session</resource>
<useridElement>kris</useridElement>
<passwordElement>kris</passwordElement>
</session>
Connecting to servlet...
Connected
Message === Sending the request to the servlet...
Complete.
Message === reading response ...
Message === java.io.FileNotFoundException: http://kmuthyal/servlet/MyServlet??xml=<?xml version="1.0" encoding="UTF-8"?>
<session>
<!--Session Element -->
<resource>session</resource>
<useridElement>kris</useridElement>
<passwordElement>kris</passwordElement>
</session>
</session>
Message === readServletResponse problem
java.io.FileNotFoundException: http://kmuthyal/servlet/MyServlet??xml=<?xml version="1.0" encoding="UTF-8"?>
<session>
<!--Session Element -->
<resource>session</resource>
<useridElement>kris</useridElement>
<passwordElement>kris</passwordElement>
</session>
</session>
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at post.Post.readServletResponse(Post.java:196)
at post.Post.init(Post.java:80)
at post.Post.main(Post.java:217)
Any suggestions?
Thanks
Kris
concern for a cleaner world, for a better tomorrow
http://www.greenpeace.org
---------------------------------
Do You Yahoo!?
Yahoo! Mail Personal Address - Get email at your own domain with Yahoo! Mail.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010604/b9ba771a/attachment.htm
More information about the jdom-interest
mailing list