To a Tag File, you don’t send request parameters, you send tag attributes!

You invoke a Tag File with a tag, and tags can have attributes. So it’s only natural that the Tag File developer might want to invoke the tag with attributes... attributes that get sent to the Tag File.

Invoking the tag from the JSP

Before (using <jsp:param> to set a request parameter)

<jsp:include page="Header.jsp">
   <jsp:param name="subTitle" value="We take the sting out of SOAP." />
</jsp:include>

After (using a Tag with an attribute)

<myTags:Header subTitle="We take the String out of SOAP" />

Using the attribute in the Tag File

image with no caption

Note

All tag attributes have TAG scope. That’s right, just the tag. Once the tag is closed, the tag attributes go out of scope!

You have to be clear about these—the <jsp:include> <jsp:param> value goes in as a request parameter. That’s not the same as a request-scoped attribute, remember. The name/value pair for the <jsp:param> looks to the web-app as though it came in with a form submission. That’s one of the reasons we DON’T like using it—the value you meant to pass ONLY to the included file, ends up visible to any component in the web app that is a part of this request (such as servlets or JSPs to which the request is forwarded).

But the nice, clean thing about tag attributes for Tag Files is that they’re scoped to the tag itself. Just be sure you know the implications. This will NOT work: ...

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.