Search code examples
htmljspentity

Escaping and unescaping HTML entities in JSP


I have a string on a .TAG file which is HTML-escaped. Now, I need to shorten this string so that the amount of 'x' is its maximum size. Then, I need to append "..." to it.

The problem is that if these "..." are escaped into an HTML-entity I get something like "$qu..." as I mess up the entity.

What is the easiest way to solve this? I thought -> decode HTML -> shorten and add "..." -> re-encode.

However, I could only find fn:escapeXML which is not quite what I want to do.

Is doing it by my own logic the only way?


Solution

  • I solved this with a scriplet. I simply imported a Java Utlity class that offers this encode and decode function.

    There is no better or smoother way than this^. The only possible better way is if you could calculate the string in the Backend. Wasnt possible in my case.

    And remember to use:

    <%@tag import="org.apache.commons.lang.StringEscapeUtils" %>
    

    Syntax for importing stuff in a .TAG file. @page import wont work here because it is not a page. That had me confused for a while and i thought its not possible to import a Java-Class into a .TAG file until i randomly figured this out.

    ^ Only way to do it using EL and JSTL is manually doing the encoding and decoding (creating an array that contains all HTML entities, fn:replace, ... .). Tons of stupid work to do, better off using one of the evil Scriplets, imho :)