ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

JSP 1.2: Great news for the JSP Community, Part 1
Pages: 1, 2, 3, 4

For all types of elements that have child nodes, the validateElement() method calls itself again with each child node. That's how the method recursively scans the whole document tree.



The real validation code in this example is found in the validateParam() method:

private void validateParam(Element e, String ns,  
  ArrayList msgs) { 
    Element parent = findParent(e, ns, "redirect"); 
      if (parent == null) { 
        String id = e.getAttributeValue("id", jspNamespace); 
        ValidationMessage vm = new ValidationMessage(id, 
          e.getQualifiedName() +  
          " must only be used with 'redirect'"); 
          msgs.add(vm); 
      } 
    }

The validateParam() method uses the findParent() method to see if the current param element has a parent element of type redirect. If it doesn't, it means that the param element is used incorrectly. In this case, a ValidationMessage instance is created to report the error and added to the list of error messages.

A ValidationMessage contains two pieces of information: the error message itself and a unique ID for the element that the message refers to. The unique ID is assigned by the container and provided to the validator as an element attribute named id in the JSP namespace; in other words, an attribute named jsp:id.

Therefore, the first thing the validateParam() method does if it finds an error is to try to get this attribute so it can include it in the ValidationMessage. This is where the Namespace instance variable mentioned earlier is used.

The container maintains a map between the ID and the location (line and column) of the element in the JSP source file. With this information, it can generate user-friendly error messages, including the location of the error. Note, however, that the container is not required to add the unique ID attributes. For instance, Tomcat 4.0 does not support this feature but future versions may.

Finally, the findParent() method looks like this:

private Element findParent(Element e, String ns, String name) {
    if (e.getName().equals(name) &&
        ns.equals(e.getNamespace().getPrefix())) {
        return e;
        }
        Element parent = e.getParent();
        if (parent != null) {
            return findParent(parent, ns, name);
        }
        return null;
  }

It simply calls itself recursively until it either finds an element of the specified type or reaches the top of the document tree. If it finds a matching element, it returns it. Otherwise it returns null.

Related Reading

JavaServer PagesJavaServer Pages
By Hans Bergsten
Table of Contents
Index
Sample Chapter
Full Description
Read Online -- Safari

JSP 1.2 still supports the validation of individual custom action elements through the TagExtraInfo isValid() method in addition to the new validation mechanism. The validation that can be performed by the isValid() method is so limited compared to what a validator can do, however, I recommend that you focus on validators from now on.

Conclusion

In this installment, I have shown a number of new features introduced by the JSP 1.2 specification that will certainly improve your applications. You can take advantage of everything in the Java 2 platform and the Servlet 2.3 API, use the include action in a much more flexible way than before, deploy tag libraries without additional configuration, use event listeners to simplify application and session state management, and create validators for your tag libraries for complete validation of JSP pages with author-friendly error messages.

But this is only the beginning. In the second part of this article I tell you all the good stuff that's been added to the custom tag developer's toolbox.

Resources

Hans Bergsten is the founder of Gefion Software and author of O'Reilly's JavaServer Pages, 3rd Edition.


Return to ONJava.com.