*
The moose likes Servlets and the fly likes send xml from jsp and read it in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "send xml from jsp and read it in a servlet" Watch "send xml from jsp and read it in a servlet" New topic
Author

send xml from jsp and read it in a servlet

Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
I have a jsp and a servlet.. which the jsp calls.
Now lets talk abt the jsp first...
It has a text area.. which initially is empty when the page is diplayed. Now i paste a "xml" document ..(not the path but the actual data).. and has a Submit button.. which calls a servlet.

On to the servlet
Now within the servlet i am trying to read the "data that was pasted in the textarea" using the request object


However now.. when i view the output.. it is not xml but simply the values read from the xml data that i had pasted in there.
I however need the xml in the servlet for further processing.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
What is "out" - where does this data go? If it goes to a web page you might want to check out its HTML source.


Ping & DNS - my free Android networking tools app
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Please post what is the input you are giving in the text area..

And what is output .. you are seeing after println ?


- Chinna
Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
OOps.. I forgot to mention that....


and the response is the HTTPResponseObject response passed to the doPost Method...

Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
This is in addition to my previous posts...

The xml file for example is :


The output that i recieve from the servlet.. after processing


where "input" is the name of the textarea in the jsp page

The output i obtain is :

The input entered is : 1.0

Which means that it only picksup the information... and the tags are omitted..

Desired output : The entire xml is the putput.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
response.setContentType("text/html");

Which means that it only picksup the information... and the tags are omitted..

So the output is indeed an HTML page. Did you follow my advice to look at the actual source of the page? I bet that it contains the complete XML you're looking for.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

When you write.. the xml into the response object.. the browser will get the following in the response...

The input entered is : <testApp>
<version>1.0</version>
</testApp>


And the response type is text/html, browser will try to execute the response as html.. then it doesn't understand the tag <testApp>/<version>.. hence it shows up only 1.0.

To display the xml as it is.. you have to escape the <,> symbols ..
ie., before writing to out.. you have to replace all "<" with "<" and all ">" with ">"
Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
@Ulf Dittmer
I totally agree with you... However the real problem is that i need the entire xml along with the tags.. inside my servlet... which would be up for further processing..
whereas at present , (already also mentioned in my last post) that the

The output i obtain is :

The input entered is : 1.0

Which means that it only picksup the information... and the tags are omitted..


I need the entire xml with tags inside my code and not "View->source"
Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
@chinna :

To display the xml as it is.. you have to escape the <,> symbols ..
ie., before writing to out.. you have to replace all "<" with "<" and all ">" with ">"


Kindly clarify
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Check this..

http://support.jodohost.com/showthread.php?t=3123
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

before writing to out.. you have to replace all "<" with "&lt;" and all ">" with "&gt;"
Muthukrishnan Manoharan
Ranch Hand

Joined: Aug 27, 2008
Posts: 91

You must escape or replace all < with & l t ; (without space) and > with & g t ; (without space)

And you will have the entire XML with tags in tact at the servlet.. So you can process them.. Just do a System.out.println and check at the servlet..
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

You're going to have use XML DOM of sorts if you want to parse the xml data. Modern browsers comprehend xml ( e.g. <> have meaning). The reason you're geting 1.0 is because the text being sent is:

?input=++%0D%0A++++<testApp>++%0D%0A++++<version>1.0<%2Fversion>++%0D%0A+<%2FtestApp>+
Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
@muthukrishnan
@chinna

The solution provided by you people works fine..:

Original :


converted to:



and the output obtained is:
The input entered is : <?xml version="1.0"?> <testApp> <version>1.0</version> </testApp>



However no "newline " between them...
Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
""
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Again..

For getting new line in html.. you need to replace "\n" with <BR> tags.
 
 
subject: send xml from jsp and read it in a servlet