1

i have a variable called param, which has the value I told \"I will come\", in JSP am assigning this to a input tag's value as

<input type="text" value="${param}"/>

what is happening is that the value am getting is I told \, i.e am getting value only till the first ", but am escaping this " with a \,but still am getting this. how can I solve this?

Seeker
  • 2,405
  • 5
  • 46
  • 80
  • You're escaping it in the JSTL, but the generated HTML will still be `value="I told "I will come""`. You need to escape it in the HTML using the `\"`, so your JSTL variable will likely need to be: I told \\"I will come\\". – Anthony Grist Jan 07 '13 at 15:45
  • @AnthonyGrist How can I achieve that? should i do while I assign the value to variable by saying something like &quot – Seeker Jan 07 '13 at 15:48
  • 1
    This is something that `fn:escapeXml()` *should* do, but I'm not sure it does. – Pointy Jan 07 '13 at 15:49
  • 2
    possible duplicate of [JSTL function to replace quote chars inside a string?](http://stackoverflow.com/questions/8898093/jstl-function-to-replace-quote-chars-inside-a-string) – BalusC Jan 07 '13 at 16:53

2 Answers2

6

Try this:

<input type="text" value="${fn:escapeXml(param)}"/>

That will work when the escaping you need is in fact XML/HTML escaping. If you're dropping the JSTL/EL expression into JavaScript code, however, that's probably not what you want. In that case you'd want a JSON encoding function, of which there are several available I think (from Google at least). (I've got my own so I can't provide direct information.)

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

If you want to stick with the Core library of JSTL, this also does the trick:

<c:out value="${param}" />
Med
  • 628
  • 9
  • 29