Stage 2 · Topic 2 - JSP · Session 3: Creating Custom Tags
| # | Where | What |
|---|---|---|
| 1 | src/<package>/MyTag.java | the tag handler class |
| 2 | WEB-INF/tags/mylib.tld | maps each tag name to its handler class and attributes |
| 3 | WEB-INF/web.xml (optional) | maps a logical URI to the TLD, if the page uses a URI instead of a path |
| 4 | any .jsp | the taglib directive plus the tag itself |
Classic — TagSupportSession 3 | Simple — SimpleTagSupportSession 7 | |
|---|---|---|
| Methods you write | doStartTag(), doAfterBody(), doEndTag() | doTag() only |
| Return values | EVAL_BODY_INCLUDE, EVAL_BODY_AGAIN, SKIP_BODY, EVAL_PAGE | none — just write to the output and return |
| Repeating the body | explicit, via doAfterBody() + EVAL_BODY_AGAIN | just a for or while loop inside doTag() |
| Getting the writer | pageContext.getOut() | getJspContext().getOut() |
| JSP version needed | 1.2 | 2.0 |
| Use it when | you must support old containers | new code — far more readable |
WEB-INF. Anywhere else a client can
download the source.setXxx() method
and an <attribute> element. Miss either and
the container cannot inject the value.static field. Tag
handler instances are pooled and reused, and statics are shared
across all concurrent requests.<body-content>empty</body-content> means the
tag may not have a body; tagdependent means the body is
passed through and the handler may look at it.