Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Newbie...Posting XML to Servlet

Reply
Thread Tools

Newbie...Posting XML to Servlet

 
 
Aparna
Guest
Posts: n/a
 
      10-10-2005
I am trying to send an XML to a SERVLET using the POST method and I run
into a problem.

I am trying to integrate a login component written by another team.
They have a test client which shows the XML that will be sent to the
servlet. Their input xml and mine matches exactly.
I know that I am forming the input XML correctly, because if I go to
http://app.com/test.jsp and replace the XML in the textbox with the XML
I create in my code, it always gives me back a good response.

I am using standard POST request in my code, nothing special, so I am
lost as to why this happens. Am I missing something here? Any help is
greatly appreciated.

input xml from my code
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Credentials>
<UserID>smhsrvuser1</UserID>
<Password>{SSHA}CoM8yMtxnTy8BR2PmOjkbe/c2vccHF+3w==</Password>
</Credentials>
<Data>
<Action>23</Action>
<ESELoginID>vrangaraj</ESELoginID>
<AppID>1,2</AppID>
</Data>
</Request>

correct output xml that I get back from the test.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<SM>
<STAT>
<CD>0</CD>
<TXT>OK</TXT>
</STAT>
<R>2^3^112</R>
<G>
<A2>8001^8100^8102^1111111111^8112^8110^8107^810 5</A2>
</G>
</SM>

incorrect output xml that I get back in the code
<?xml version="1.0" encoding="UTF-8"?>
<SM>
<STAT>
<CD>999</CD>
<TXT>Invalid input xml</TXT>
</STAT>
</SM>
----------------------------------------
code that I use to send the request


code:
--------------------------------------------------------------------------------

//forming the document
Document doc=null;
String inputxml = null;
try {
doc = DocumentHelper.parseText("<?xml version=\"1.0\"
encoding=\"UTF-8\"?><Request></Request>");
}
catch (DocumentException e)
{ e.printStackTrace(); }
//forming the xml
Element credElem = doc.getRootElement().addElement("Credentials");
Element usrElem = credElem.addElement("UserID");
if(usrElem!=null)
usrElem.setText("smhsrvuser1");
Element pswElem = credElem.addElement("Password");
if(pswElem!=null)
pswElem.setText("{SSHA}PmOjkbe/c2vccHF+3w==");
Element datElem = doc.getRootElement().addElement("Data");
Element actElem = datElem.addElement("Action");
actElem.setText("23");
Element logElem = datElem.addElement("ESELoginID");
logElem.setText("marc");
Element appElem = datElem.addElement("AppID");
appElem.setText("1,2");
inputxml = doc.asXML();
//sending request to the servlet
serviceURL="http://app.com/Login";
URL url = new URL(serviceURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

wr.write(inputxml);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{ retVal = retVal + line; }
wr.close();
rd.close(); }
catch (Exception ex) { }
//this always gives me the Invalid Input XML error
System.out.println("\n"+retVal);

 
Reply With Quote
 
 
 
 
HalcyonWild
Guest
Posts: n/a
 
      10-10-2005

Aparna wrote:
> I am trying to send an XML to a SERVLET using the POST method and I run
> into a problem.
>
>
> I am using standard POST request in my code, nothing special, so I am
> lost as to why this happens. Am I missing something here? Any help is
> greatly appreciated.
>
> //forming the document
> Document doc=null;
> String inputxml = null;
> try {
> doc = DocumentHelper.parseText("<?xml version=\"1.0\"
> encoding=\"UTF-8\"?><Request></Request>");
> }
> catch (DocumentException e)
> { e.printStackTrace(); }
> //forming the xml
> Element credElem = doc.getRootElement().addElement("Credentials");
> Element usrElem = credElem.addElement("UserID");
> if(usrElem!=null)
> usrElem.setText("smhsrvuser1");
> Element pswElem = credElem.addElement("Password");
> if(pswElem!=null)
> pswElem.setText("{SSHA}PmOjkbe/c2vccHF+3w==");
> Element datElem = doc.getRootElement().addElement("Data");
> Element actElem = datElem.addElement("Action");
> actElem.setText("23");
> Element logElem = datElem.addElement("ESELoginID");
> logElem.setText("marc");
> Element appElem = datElem.addElement("AppID");
> appElem.setText("1,2");
> inputxml = doc.asXML();
> //sending request to the servlet
> serviceURL="http://app.com/Login";
> URL url = new URL(serviceURL);
> URLConnection conn = url.openConnection();
> conn.setDoOutput(true);
> OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
>
> wr.write(inputxml);
> wr.flush();
> // Get the response
> BufferedReader rd = new BufferedReader(new
> InputStreamReader(conn.getInputStream()));
> String line;
> while ((line = rd.readLine()) != null)
> { retVal = retVal + line; }
> wr.close();
> rd.close(); }
> catch (Exception ex) { }
> //this always gives me the Invalid Input XML error
> System.out.println("\n"+retVal);



Can you post at which line the exception is thrown. YOu are catching
Exception e and that just about catches everything that is thrown its
way.

Also, are you using some other DOM parsing framework, apart from
org.w3c.dom.* classes. Looks like JDOM to me.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping session with Servlet to Servlet communications DiscoStu Java 0 07-08-2004 06:50 PM
Offending class: javax/servlet/Servlet.class chamikara Java 1 06-19-2004 05:42 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM
how to get my servlet configuration before the servlet is initialised Andy Fish Java 4 12-17-2003 09:47 PM



Advertisments