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>.

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.