John
John

Reputation: 586

How can I escape both quotation mark and backslash in jsp?

I need to escape " and \ in jsp, but not necessarily in the same time.

With <c:out var="${value}" /> I can escape " character.

With ${fn:replace(value,'\\','&#92;')} I can escape the \ character, and is working fine.

I tried to handle both cases as follows.

I tried to use

<c:out var="${fn:replace(value,'\\','&#92;')}"/>

but is not working, is not accepted, seems to be an error.

I also tried to put in a variable the string after replace, and after that using it in c:out, but was the same thing.

If anyone has an idea of a way to handle both cases, please let me know.

Note: the input comes from Java, that's why I used \\, and is sent forward as JSON. Thanks

Upvotes: 2

Views: 3972

Answers (2)

John
John

Reputation: 586

I found the best solution to be StringEscapeUtils from Apache, and I had to create a taglib as @Tap said, and added jar file commons-lang to project . This has a lot of functions for working with strings.

More details can be found here ->

Upvotes: 1

Tap
Tap

Reputation: 6522

You could call fn:replace() twice, chaining the calls, to replace both characters.

${fn:replace(fn:replace(value,'\\','&#92;'),'\"','&#34;')}

It's not very pretty, though.

Upvotes: 3

Related Questions