Search code examples
jspcustom-tagsscriptlet

jsp scriplet text appears as is in jsp output when using custom tags


I have two custom tags mbar.tag and mitem.tag that are used as follows:- test.jsp

<html>
<body>
<% String sArg = request.getParameter("someparam"); %>
<mytags:mbar>
    <mytags:mitem title="images/<%= sArg %>-first.png"/>
    <mytags:mitem title="images/<%= sArg %>-second.png"/>
</mytags:mbar>
</body>
</html>

mbar tag evaluates to a element mitem tag evaluates to a element with value of title as body of the element They are declared in a tld file

Output of test.jsp is

<html>
<body>
<% String sArg = request.getParameter("someparam"); %>
<div>
    <div>images/**<%= sArg %>**-first.png</div>
    <div>images/**<%= sArg %>**-second.png</div>
</div>
</body>
</html>

The result contains the scriplet verbatim whereas I require the value of sArg to be substituted. No errors are returned on screen. Seeing that jsp scriplets are supported in jsp, I am sure it is something to do with the custom tags. What am I missing?


Solution

  • Don't use scriptlets. Never. Custom tags and the JSP EL have been introduced to be able to avoid scriptlets.

    Use the JSP EL:

    <mytags:mitem title="images/${param.someparam}-first.png"/>
    

    And make sure that your tag's title tag is declared as accepting runtime expressions, using

    <rtexprvalue>true</rtexprvalue>
    

    in the attribute declaration of the TLD file.