Question

I'm making some JSP tag. I pass a type parameter to the tag.

The code look like this:

<%@ attribute name="type" require="true" %>
<c:choose>
    <c:when test="${type eq 'even'}">
        <c:set var="remainder" value="0" />
    </c:when>
    <c:when test="${type eq 'odd'}">
        <c:set var="remainder" value="1" />
    </c:when>
    <c:otherwise>
        <%-- Want to throw exception!! --%>
    </c:otherwise>
</c:choose>

I want to throw an exception if I pass a wrong value. When I searched about this subject I just found this. Can't I throw a normal exception in JSTL?

Was it helpful?

Solution

Just throw to notice the problem immediately

Who would "notice" ? That exception can only be caught by the container and it will result in some "500 Internal Server Error" page (or some custom error page you define).

If that's what you really want, you can define a custom tag that will just perform "throw new WhateverException()", and then make use of your custom taglib like: <mytaglib:reportError/>, as the standard core taglib did not consider throwing an exception would ever be desirable for anyone.

OTHER TIPS

I have decided to revive and edit a deleted answer which looked partially correct.

You can throw an exception by adding it in a scriptlet like so:

<c:choose>
    <c:when test="${type eq 'even'}">
        <c:set var="remainder" value="0" />
    </c:when>
    <c:when test="${type eq 'odd'}">
        <c:set var="remainder" value="1" />
    </c:when>
    <c:otherwise>
        <% if (true) throw new Exception("Wrong type"); %>
    </c:otherwise>
</c:choose>

However, your jsps are supposed to be part of your "view" layer so you should really try to keep your logic (including throwing the exception) in your servlets.

Note the if (true) statement. It is a trick becase the compiler will complain about "unreachable code" if you just drop the exception scriptlet into your code.

I would just use a custom EL function or tag which systematically throws an exception, or displays a big red div with the error message, or both

${myFn:illegalState("the type must be odd or even but it is " ${type})}

I'll assume your goal here is to inform the developer of their improper use of your custom tag. As alluded to in the other answers, you would want to handle user input errors in a different manner as throwing an error in the JSP will probably (depending on configuration) end up sending the user to the application error page.

If you are able to code your tag handler in Java, the spec includes a couple of optional elements in the TLD which can do this.

  1. The spec defines the validator element which takes a class implementing javax.servlet.jsp.tagext.TagLibraryValidator. In practice this is somewhat complicated to use as it offers a bunch of flexibility.
  2. The spec also defines tei-class which takes a class extending TagExtraInfo. As the JavaDoc notes:

    Extra Tag Information for a Custom Tag; this class is mentioned in the Tag Library Descriptor file (TLD). This class must be used:

    • if the tag defines any scripting variables
    • if the tag wants to provide translation-time validation of the tagattributes.

Depending on the JSP version, you would need to override isValid() or validate() method to handle validating the variables provided by the developer.

The Apache taglibs use these for validating some of their tags: (https://github.com/javaee/jstl-api/blob/master/impl/src/main/java/org/apache/taglibs/standard/tei/ImportTEI.java)

    public boolean isValid(TagData us) {
        if (Util.isSpecified(us, VAR) && Util.isSpecified(us, VAR_READER))
            return false;

        return true;
    }

OR if you are using JSP 2.0, implementing validate is a little more complex as it allows returning an Array of ValidationMessage. Glassfish has an example:(https://github.com/javaee/glassfish/blob/master/appserver/tests/appserv-tests/devtests/web/jspGetTagLibraryInfos/servlet/taglib/MyTagExtraInfo.java)

    public ValidationMessage[] validate(TagData data) {
        ValidationMessage[] vms = null;
        TagLibraryInfo[] infos =
            getTagInfo().getTagLibrary().getTagLibraryInfos();
        if (infos.length != 1) {
            vms = new ValidationMessage[1];
            vms[0] = new ValidationMessage(null, "Wrong number of tsglibs");
        }
        return vms;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top