[jdom-interest] help !!!!!

Matthew Mackenzie matt at xmlglobal.com
Mon Jun 4 22:36:38 PDT 2001


The problem is that the url is not well formed (new lines, etc.).  What
you should do is use a HTTP POST to send the XML request.  Why you
shouldn't use GET:
 
- you can't have newlines in a URL, so you would have to strip them out.
If you strip out the newline after <?xml...?>, you might have problems
parsing the string.  Assuming you overcame that problem, you really
should be URL encoding the string before adding it to the URL.
 
- Web servers probably set some realistic limits on the maximum size of
a GET request, so you might see server errors.
 
If you MUST use GET....
 
- Get rid of the xml pi, and pray that your parser isn't strict.
- Get rid of new lines.
- use URLEncoder.encode(...)
 
 
Cheers,
 
Matt
 
 

	-----Original Message----- 
	From: krishna muthyala 
	Sent: Mon 6/4/2001 3:27 PM 
	To: jdom-interest at jdom.org 
	Cc: 
	Subject: [jdom-interest] help !!!!!
	
	

	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);
	      &nb! sp;           
	         // 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("password!
Element").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();
	        }
	        
	         &nb! sp;              
	        //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);
	         
	         ou! tputToServlet.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.getS!
erverName()+":"+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=
<http://kmuthyal/servlet/MyServlet??xml=>  version=" target=_blank ?>
	<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=
<http://kmuthyal/servlet/MyServlet??xml=>  version=" target=_blank ?>
	<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
<http://personal.mail.yahoo.com/?.refer=mailiyfoot>  - Get email at your
own domain with Yahoo! Mail.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 25098 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20010604/6731a118/attachment.bin


More information about the jdom-interest mailing list