O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using <c:set> with beans and Maps

This flavor of <c:set> (with its two variations—with and without a body) works for only two things: bean properties and Map values. That’s it. You can’t use it to add things to lists or arrays. It’s simple—you give it the object (a bean or Map), the property/key name, and the value.

Setting a target property or value with <c:set>

  1. With NO body

    image with no caption
  2. WITH a body

    image with no caption

Note

The “target” must evaluate to the OBJECT! You don’t type in the String “id” name of the bean or Map attribute!

This is a huge gotcha. In the <c:set> tag, the “target” attribute in the tag seems like it should work just like “id” in the <jsp:useBean>. Even the “var” attribute in the other version of <c:set> takes a String literal that represents the name of the scoped attribute. BUT... it doesn’t work this way with “target”!

With the “target” attribute, you do NOT type in the String literal that represents the name under which the attribute was bound to the page, scope, etc. No, the “target” attribute needs a value that resolves to the REAL THING. That means an EL expression or a scripting expression (<%= %>), or something we haven’t seen yet: <jsp:attribute>.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required