It's not a secret anymore!*
The moose likes JSP and the fly likes Double Quotes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Double Quotes" Watch "Double Quotes" New topic
Author

Double Quotes

Deepan Ignatius
Ranch Hand

Joined: Mar 29, 2010
Posts: 41
Hi,

I have the following prece of JSP code where I am trying to access a hashMap varaible present in session scope and try to create as many as hidden variables possible.



Problem : When the ${entry.key} has double quotes as part of its value then it is rendered differently.
Ex:

if the key is abc"def and value is 33 then a hidden variable should be created with the key abc"def and value as 33 but created as key abc and value as 33.

Although this could be resolved by changing the 2nd line of code to
<input type='hidden' name='${entry.key}' value='${entry.value}' />

Is there a better solution to this?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

The internal quotes need to be encoded.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Deepan Ignatius
Ranch Hand

Joined: Mar 29, 2010
Posts: 41
could you help me how to do this?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
You need to replace quotes in your key/value with "&quot;" so that it renders as a quote in html, without disturbing the quotes you are putting the value in.
Quite possibly the JSTL replace function would do the trick



Using a framework such as struts/stripes etc will normally do this sort of thing for you as well.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Or simply use <c:out> or ${fn:escapeXml()}

Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Yeah. Do that. Thats even easier.
Serves me right to try and answer a question before properly waking up. Somehow I got the thought that it wouldn't escape things properly, but thats dealing with javascript strings, which are an entirely different kettle of fish.




Deepan Ignatius
Ranch Hand

Joined: Mar 29, 2010
Posts: 41
Thanks for your suggesstions.

I tried to use <cut> and below is the piece of code which I changed

before:

<input type="hidden" name="${entry.key}" value="${entry.value}"/>

after:

<input type="hidden" name=<cut value="${entry.key}" escapeXml="false"/> value=<cut value="${entry.value}" escapeXml="false"/> />

Even with this, the hidden varaibles are not created properly.

ex:-
if the entry.key contains any double quotes say Nok"123 then hidden varaible is created with the name Nok and not Nok"123 which is what I am trying to do.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

That's because your HTML is sloppy. Always quote attribute values. This has nothing at all to do with JSP.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
It might have something to do with the fact that you have specified escapeXml as false !?!?!?
Escaping the XML is the whole point of using this tag!!! That's what will convert the " to &quot;
Also Bear's comment about quotes is also relevant.
I would use single quotes in this case. Some parsers have trouble with 'nested' double quotes even if the quotes in question are in a seperate tag.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Stefan Evans wrote:It might have something to do with the fact that you have specified escapeXml as false !?!?!?

That too!
 
 
subject: Double Quotes
 
Similar Threads
JSTL killing me
Baffling NPE in <c:forEach
escape single quote in input type=hidden
how to print values of a hashmap using logic:iterate
Accessing session scope attributes