On Thu, 21 Mar 2002, Matt Raible wrote:

> Is it possible to do the following with JSTL?
> 
> <c:if test="${cookie.username == 'learner'}">
>     learner
> </c:if>

Ah, I was hoping someone would ask.  The answer is "no, not exactly like
that," but you can still use JSTL to access cookies.

While web developers often have the urge to access a cookie by name, it's
an urge that's best to resist; cookies aren't identified by name alone,
but by name, domain, path, and security status.  Thus, the best way to
access a cookie is to loop over the list of available cookies, matching
the one that you're interested in.

Now, if you're just interested in name, that's fine; but JSTL doesn't go
out of its way to support that special case.  Instead, you can just write

  <c:forEach items="${pageContext.request.cookies}" var="cookie">
    <c:if test="${cookie.name == 'learner'}">
      <c:set var="cookieValue" value="${cookie.value}" />
    </c:if>
  </c:forEach>

I explain this in more detail in my book; interestingly enough, I just
wrote the section describing techniques like this.

-- 
Shawn Bayern
Author, "JSP Standard Tag Library"  http://www.jstlbook.com
(coming this summer from Manning Publications)


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to