This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
There are two types of Custom tags that can be used in JSP 2.0 environment,Classic and Simple. A simple tag can be nested in a classic as well as a simple tag i.e,A simple tag can have Classic as well as Simple Parent. But a Classic tag mostly has a classic tag as a parent.
The reason I came to know is as follows:
getParent() method, in case of Classic tag returns Tag type of object Which is difficult to cast into SimpleTag.Why so?
Again, We can cast from Tag to the tag handler class in case of classic tag which should not happen since Classic tag Handler is a subclass of tagSupport which implements Tag.(SuperClass to Subclass cast)
Same is the case for SimpleTag only SimpleTagSupport and JspTag replace Tag and TagSupport (superclass to subclass cast)
SimpleTag.getParent() returns JspTag, Tag.getParent() returns Tag. Now Tag and SimpleTag both extends JspTag, so from SimpleTag.getParent() both objects implementing Tag and SimpleTag can be returned. However, SimpleTag is not compatible with Tag. That means that Tag.getParent() cannot return a SimpleTag. Casting won't help you out because that will usually fail with a ClassCastException. The solution that is provided is to use TagAdapter.
When a (classic) Tag is placed inside a SimpleTag, then the Tag returned by Tag.getParent() will be a TagAdapter. You can check for that, and then cast to it and retrieve the SimpleTag from it: