SkipPageException

If anything goes wrong in a tag, we should react throwing an exception, delegating to the JSP page that made the call the choice of reacting to this exceptional circumstance as it feels better.

We know how to let the container to generate an error page in case of exception in a JSP page, and this could be an acceptable way to address this issue.

Another way of reacting could be checking in the JSP page the JspException thrown by the tag using a JSTL c:catch tag.

And, the tag could throw a SkipPageException that would stop the creation of the JSP page at the point where the exception is generated.

Here is our test tag implementation:
package ch10;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTagTest6 extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().print("Message from within doTag().");
    //  throw new SkipPageException();
        throw new JspException();
    }
}
You could check the different tag behavior if it throws a SkipPageException or a JspException.

Before using it, we should edit the TLD file adding its declaration:
<tag>
    <description>skip page exception</description>
    <name>simple6</name>
    <tag-class>ch10.SimpleTagTest6</tag-class>
    <body-content>empty</body-content>
</tag>
Now we can put in a JSP these lines:
<%@ taglib prefix="st" uri="simpleTags" %>
<!-- ... -->
About to invoke a tag that throws SkipPageException ...
<st:simple6/>
... back in the page after invoking the tag.
<!-- ... -->
We'll get the text generated by the tag till the exception is generated, and we won't get all the text after the "bad" tag in the JSP page.

Much more stuff on Simple tag handlers in chapter ten of Head First Servlet and JSP.

No comments:

Post a Comment