Eval taglib ver. 1.9

    Custom JSP taglib. Tag eval allows you to evaluate JSP code in the dynamic. It is similar to the function eval() you may see for example in JavaScript and ColdFusion. So, you may accumulate any JSP code in the string variable and evaluate (execute) this JSP code.

For example:
Suppose you need to execute this JSP code:
 


<%
    int i=1; i++; out.println("<br>i="+i);
%>

<%@ taglib uri="taglib.tld" prefix="exec" %>
<html>

<%
   String s="&lt;% int i; i++; out.println(\"<br>i=\"+i);%&gt;";
%>

<exec:eval code="<%=s%>" />
...
</html>

Note: you have to use &lt; instead of < and &gt; instead of > in the string for JSP elements.

Tag eval can cache dynamic code. So if you are executing the same code several times you can avoid recompilations. In order to use cache you have to provide an additional parameter cacheDir. This parameter describes some existing directory in the file tree of your web application. Note: you have to provide here a path to this directory relatively to your jsp files. E.g. if you are using a subdirectory EVAL_CACHE within subdirectory DATA you can write so:
 


<exec:eval code="<%=your_JSP_code%>" cacheDir="DATA/EVAL_CACHE"/>

Tag evalPage lets you evaluate JSP page described in the parameter src. Without the parameter id it is just an analogue of jsp:include. With parameter id you can save results (rendered page) in the page scope variable (type is java.lang.String).

Body tag evalBody lets you evaluate own body twice. Suppose your JSP code (custom tags) outputs some JSP code (e.g. with scriptlets, tags etc.) rather than plain HTML. In this case you need to evaluate (execute) this output again. That is what evalBody tag is for. For example:
 



<%
   String s="out.println(\"Hello\")";
%>

<exec:evalBody>
   <% out.println("<"+"% "+s+";%"+">"); %>
</exec:evalBody>

Tags are:

eval

Executes JSP code from the parameter. Parameter is:

1) code JSP code
2) cacheDir Optional parameter. Describes a directory for cache. By default is empty (tag does not use cache).
3) cond Optional parameter. Describes a boolean value tag's behavior depends on. Default value is true.

evalPage

Executes JSP page. Parameters are:

1) src describes your JSP page
2) id Optional parameter. Describes a page scope variable for the result. By default tag just prints data.
3) cond Optional parameter. Describes a boolean value tag's behavior depends on. Default value is true.

evalBody

Body tag executes own body twice. Parameters are:

1) id Optional parameter. Describes a page scope variable for the result. By default tag just prints data.
2) cond Optional parameter. Describes a boolean value tag's behavior depends on. Default value is true.
 

for downloading:

Library: evaltag.jar    Description: taglib.tld

© Coldbeans Software      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags: