Describe the semantics of the "Simple" custom tag event model when the event method (doTag) is executed; write a tag handler class; and explain the constraints on the JSP content within the tag.

JSP 2.0 introduces a new type of tag extension called a Simple Tag Extension. Simple Tag Extensions can be written in one of two ways:

In addition to being simpler to work with, Simple Tag Extensions do not directly rely on any servlet APIs, which allows for potential future integration with other technologies. This is facilitated by the JspContext class, which PageContext now extends. JspContext provides generic services such as storing the JspWriter and keeping track of scoped attributes, whereas PageContext has functionality specific to serving JSPs in the context of servlets. Whereas the Tag interface relies on PageContext, SimpleTag only relies on JspContext. Unlike classic tag handlers, SimpleTag does not extend Tag.

package javax.servlet.jsp.tagext;
					
public interface SimpleTag extends JspTag {
	public void doTag()throws JspException, java.io.IOException;
	public void setParent(JspTag parent);
	public JspTag getParent();
	public void setJspContext(JspContext pc);
	public void setJspBody(JspFragment jspBody);
}
					

Most SimpleTag handlers should extend javax.servlet.jsp.tagext.SimpleTagSupport. This is the convenience class, similar to TagSupport or BodyTagSupport. There are also some helpful methods included in this class that include:

The body of a Simple Tag, if present, is translated into a JSP Fragment and passed to the setJspBody method. The tag can then execute the fragment as many times as needed. Because JSP fragments do not support scriptlets, the <body-content> of a SimpleTag cannot be "JSP". A TLD is invalid if it specifies "JSP" as the value for <body-content> for a tag whose handler implements the SimpleTag interface. JSP containers are recommended to but not required to produce an error if "JSP" is specified in this case.

During the translation phase, various pieces of the page are translated into implementations of the javax.servlet.jsp.tagext.JspFragment abstract class, before being passed to a tag handler.

JSP fragments are pieces of JSP code. Written using standard JSP syntax, a fragment is translated into an implementation of the interface JspFragment for use by tag handlers. A JSP fragment is used to represent the body of a tag for use with a SimpleTag handler. The JspFragment interface declares only two methods: invoke and getJspContext.

package javax.servlet.jsp.tagext;

public abstract class JspFragment {
	public abstract void invoke(java.io.Writer out)
		throws JspException, java.io.IOException;
	public abstract JspContext getJspContext();
}
					
invoke causes the fragment to be executed, writing the output produced to the Writer passed to it. You can invoke a fragment as many times as needed. invoke can throw a SkipPageException, which signals that the fragment has determined that the remainder of the page doesn't need to be evaluated.

The following example shows the simple custom tag:

public class MySimpleTag extends SimpleTagSupport {
	public void doTag()	throws JspException, IOException {
		getJspContext().getOut().write("Hello, World !");
	}
}					
					

The lifecycle of a Simple Tag Handler is straightforward and is not complicated by caching semantics. Once a Simple Tag Handler is instantiated by the Container, it is executed and then discarded. The same instance must not be cached and reused.

The following lifecycle events take place for the simple tag handler (in the same order):

  1. A new tag handler instance is created each time the tag is encountered by the container. This is done by calling the zero argument constructor on the corresponding implementation class. It is important to note that a new instance must be created for each tag invocation.

  2. The setJspContext() and setParent() methods are invoked on the tag handler. If the value being passed is 'null', then the setParent() method need not be called. In the case of tag files, a JspContext wrapper is created so that the tag file can appear to have its own page scope. Calling getJspContext() must return the wrapped JspContext.

  3. The container calls the setters for each attribute defined for this tag in the order in which they appear in the JSP page or Tag File. If the attribute value is an expression language expression or a runtime expression, it gets evaluated first and is then passed on to the setter. On the other hand if the attribute is a dynamic-attribute then setDynamicAttribute() is called.

  4. The setJspBody() method is called by the container to set the body of this tag, as a JspFragment. A value of null is passed to setJspBody() if the tag is declared to have a <body-content> of empty.

  5. The doTag() method is called by the container. All tag logic, iteration and body evaluations occur in this method.

  6. All variables are synchronized after the doTag() method returns.

The following example simply writes the body to the output stream. When the Writer given to invoke is null, the output from invoke goes to the JspWriter associated with the JspContext of the tag handler:

					
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class MySimpleTag extends SimpleTagSupport {
	public void doTag() throws JspException, IOException {
		getJspBody().invoke(null);
	}
}
					
The invoke method directs all output to a supplied writer or to the JspWriter returned by the getOut method of the JspContext associated with the tag handler if the writer is null.

<%@ taglib uri="/mytag" prefix="mytag" %>
<html>
	<body>
		<mytag:MySimpleTag>
			Hello, World !
		</mytag:MySimpleTag>
	</body>
</html>
					
					

SimpleTagSupport provides the convenience method getJspBody() to return the JspFragment generated for the body content. The following example demonstrates that a SimpleTag can obtain a copy of the body so that it can use or MANIPULATE it:

public class MySimpleTag extends SimpleTagSupport {
	public void doTag() throws JspException, IOException  {
		StringWriter sw = new StringWriter();
		getJspBody().invoke(sw);
		getJspContex().getOut().write(sw.toString());
	}
}					
					
is equivalent to:
public class MySimpleTag extends SimpleTagSupport {
	public void doTag() throws JspException, IOException  {
		StringWriter sw = new StringWriter();
		jspBody().invoke(sw);
		getJspContex().getOut().write(sw.toString());
	}
}					
					

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide