Key points and gotchas with <c:set>

Yes, <c:set> is easy to use, but there are a few deal-breakers you have to remember...

Note

  • You can never have BOTH the “var” and “target” attributes in a <c:set>.

  • “Scope” is optional, but if you don’t use it the default is page scope.

  • If the “value” is null, the attribute named by “var” will be removed!

  • If the attribute named by “var” does not exist, it’ll be created, but only if “value” is not null.

  • If the “target” expression is null, the Container throws an exception.

  • The “target” is for putting in an expression that resolves to the Real Object. If you put in a String literal that represents the “id” name of the bean or Map, it won’t work. In other words, “target” is not for the attribute name of the bean or Map—it’s for the actual attribute object.

  • If the “target” expression is not a Map or a bean, the Container throws an exception.

  • If the “target” expression is a bean, but the bean does not have a property that matches “property”, the Container throws an exception. Remember that the EL expression ${bean.notAProperty} will also throw an exception.

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.