This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes runtime encoding probolem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "runtime encoding probolem" Watch "runtime encoding probolem" New topic
Author

runtime encoding probolem

Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi folks
In my jsp iam going to populate special characters (<>(*&), it is
working fine, but if i substring that one to certain limit(for ex:10)
for example if i give like --> &<> to populate in jsp it is
taking as --> &<> and it is going to substring and it is
taking upto--> &<&
but the xml parser not able to parse and thrown NotWellFormed exception
how can i overcome this one.
Advance appreciation for any ideas..

-Naresh
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
It is usually safer and less error-prone to send any data containing special characters to be encoded before flushing it to the client. You can find a list of special character mappings here. If you rely on sending data as-is, then you can never be quite sure as to how different browsers might interpret special characters as. Some will render it properly or some might engage in escaping the characters on their own.
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi Anirvan
thank you for valuable information,as you said i tried before
rendering...
in my java file i decode special characters using URLDecoder like

but it is not decoding and same encoded data is populating..
can you please let me know how can i do..

-Naresh
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Let me try and understand what your requirement is:
1. You are sending the string value=> &*<> from your server to the client.
2. The client is rendering &*<> fine, but when you do a substring on it, you're not getting the correct value. What is the index parameter you pass for substringing and what do you get as a result?
3. Where is the XML parser being used? Is it at the server side or the client side [using JavaScript]?
If it is at the server side, then you need to escape characters like=> &, < and >. Otherwise your parser is bound to throw an error.

And why are you using URLDecoder to "decode" the string before sending it to the client? Firstly, I don't think that you really need to escape special characters when displaying simple HTML output through a JSP. And secondly, you should be "encoding" and not "decoding" when you send the content to the client! The browsers are capable enough to decode on their own.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
I don't understand where an XML parser is involved in using a JSP page, but unless those characters are part of an URL, URLEncoder/URLDecoder have nothing to do with this.

The way to transport special characters in XML is either to replace them by their escaped entities (e.g. & --> &amp; and < --> &lt;) or to wrap them in CDATA sections.

In JSP pages you may wish to investigate the JSTL <cut> tag and its escapeXml attribute.


Ping & DNS - my free Android networking tools app
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi Anirvan
-->iam using RICO ajax ,in the jsp iam getting the vales from java file and iam going to populate it, while it is done it is parsing characters and populating in jsp.
-->in the substring function i had given like
iam getting Exception like..
XML Parsing Error: not well-formed

--> XML parser is used in javascript file like rico.js RICO ajax
there they had used DOMParser
--> In my java file iam replacing special characters using string.replaceAll(), and rendering to JSP

previously i havent done replacing , but now iam replacing and rendering the actual value but the xml parser again parsing like <> and populating in JSP. if i dont used substring the out put is well, but if i used <substring> it is parsing upto certain characters and giving parse exception..
[ May 06, 2008: Message edited by: Naresh Talluri ]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Alright, so you're using Rico.js [not that I've used it ever] and then, you're also using a certain taglib tag : <str:substring start="0" end="18"/>. Again you don't mention which taglib this is, 'coz I certainly can't recall ever coming across such a taglib tag, which allowed me to do so.

Just a word of advice Naresh, you should try and provide as much information as possible so that those who try and help you out are in a better position to do so. Letting out information in installments serves no purpose.

Anyways, getting back to your problem, even though I can't comment upon the functioning of that unknown tag, I can only make a guess that it tries to substring the current string value which you print through the <cut/> tag. I have a feeling that your substring tag works somewhat like a XSLT's template, where it takes the current string value enclosed between two tags and operates upon it. And since the characters "<" or ">" appear in that value some where, it retrieves a value which doesn't conform to something it expects.

Try this: Before sending the response to the JSP, just do a replace all on "<", ">" and "&", and replace them with a " " [space] character. If the error is removed this way, then it's the problem I've cited above. In that case, try the CDATA approach Ulf has suggested in his post.
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
I had tried using CDATA but here i can avoid that error but it is not populating in JSP, because the XMLParser recognises onlyparsed elements, if i give like CDATA we are mark it as (dont parse).
And iam using taglibs-string.jar,jstl1.2.jar,standard.jar.
If we give ("") empty string whenever <,>,& occur, it should work fine.
coz parser wont find any special characters,if it find any special characters then only it should trying to parse..
and i tried almost all possibilities which i know, and iam not able to solve then decided to post.
[ May 06, 2008: Message edited by: Naresh Talluri ]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
So well,
it seems atleast we've got one part right that the special "str" tag does in fact function upon the string value enclosed by the XML tag it's operating on. However I'm not too sure whether you're using a standard taglib. If you can post the taglib directive at the top of your JSP corresponding the "str" tag, then may be it might help us understand how does it actually work.

Going through the previous posts, I couldn't make out whether you've tried replacing => "<" with "<", ">" with ">" and "&" with "&" using the replaceAll() method in your server. Let us know if that helped.
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Originally posted by Anirvan Majumdar:
So well,
it seems atleast we've got one part right that the special "str" tag does in fact function upon the string value enclosed by the XML tag it's operating on. However I'm not too sure whether you're using a standard taglib. If you can post the taglib directive at the top of your JSP corresponding the "str" tag, then may be it might help us understand how does it actually work.

Going through the previous posts, I couldn't make out whether you've tried replacing => "<" with "<", ">" with ">" and "&" with "&" using the replaceAll() method in your server. Let us know if that helped.



Iam using standard taglib
here is my code...


and previously in my java fiel iam replacing like
fieldValue.replaceAll("<","<");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: runtime encoding probolem