<%@ page import="java.io.File" %> <%@ page import="java.util.Date" %> <%@ taglib uri="gnat" prefix="gnat" %> <%-- Use the Servlet 2.2+ temporary working directory to demo some file stuff. Makes it easier because the user doesn't have to configure anything and I don't have to worry about file system and user permissions for the demo. See section 3.7 of Servlet 2.3 spec for description of this attribute. --%> <%! File tempdir = null; %> <% tempdir = (File)application.getAttribute("javax.servlet.context.tempdir"); %> Gnat JSP Taglib

Gnat Taglib: echo

The Echo tag writes a message to the JSP output stream. The Echo tag can also take body content instead of a message attribute. The file and append attributes are used to write messages to a file.

example 1 - echo a simple message to JSPWriter out.

<gnat:echo message=""/> :

<%-- Until JRun and Jasper escape String literals nested in custom tag attributes properly, it's best to declare them as variables and then use their variable names rather than the literal strings. The multitude of double quotations seems to confuse JRun (3.0, SP1) and Jasper (3.3.dev), though JRun can deal with them if the quotes around the whole attribute are removed. --%> <%-- Name of file to create in demo --%> <% String echoLog = tempdir+"/echoAppendLog.txt"; %> <% Date time = new Date(); %> <% String messageString = "Hello World. The time is: "+time+System.getProperty("line.separator"); %>

example 2 - echo and append to logfile

<gnat:echo message="<%= messageString %>" append="true" file="<%= echoLog %>" />

example 3 - echo to logfile, do not append

<% String echoLog2 = tempdir+"/echoNoAppendLog.txt"; %>

<gnat:echo message="<%= messageString %>" file="<%= echoLog2 %>" />

example 4 - echo to JSP output, no attributes

<gnat:echo>
Hello, here is a message! (and a dynamic date: <%= time %>).
</gnat:echo>

Hello, here is a message (and a dynamic date: <%= time %>).