This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

send xml from jsp and read it in a servlet

 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "out" - where does this data go? If it goes to a web page you might want to check out its HTML source.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post what is the input you are giving in the text area..

And what is output .. you are seeing after println ?
 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps.. I forgot to mention that....


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

 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this..

http://support.jodohost.com/showthread.php?t=3123
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before writing to out.. you have to replace all "<" with "&lt;" and all ">" with "&gt;"
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
""
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again..

For getting new line in html.. you need to replace "\n" with <BR> tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic