cookies in JSTL
Hi,
In my index.jsp, there are 3 fields - email text, password text & 'remember me' checkbox. When the user has entered email & password and selected 'remember me' checkbox, I need to authenticate the email/password combination & put the email in browser's cookie.
I am able to authenticate as well as set up the cookie, but the problem is really on displaying the cookie value. Here is the code -
LoginAction.java
Cookie emailCookieObj = new Cookie("emailCookie", form.getAccountEmail());
emailCookieObj.setMaxAge(60*60*24);//store the cookie for a 24 hr period
httpServletResponse.addCookie(emailCookieObj);
index.jsp
<c:if test="${empty cookie.emailCookie}">
<html:text name="logonForm" property="accountEmail" value="" styleId="email" styleClass="txt"/>
</c:if>
<c:if test="${!empty cookie.emailCookie}">
<bean:cookie id="emailCookie" name="emailCookie"/>
<html:text name="logonForm" property="accountEmail" value='<%=emailCookie.getValue()%>' styleId="email" styleClass="txt"/>
</c:if>
Surprisingly, both <c:if... > statements work when the cookie is set; and both fail when cookie is removed from the browser. One would think that one of them should work when cookie is available (or removed).
It would be great if anyone can point out where my mistake is!
Thanks in Adv...
[1463 byte] By [
MrReddya] at [2007-11-27 8:28:45]

# 3
But instead of using <%=emailCookie.getValue()%> in the value attribute of <html:text>, is there a way to set the value using JSTL?
I tried the following, without any success -
1. replacing by <c:out value="${emailCookie.value}"/>
2. replacing by <c:out value="${emailCookie.value.value}"/>
3. using
<bean:cookie id="emailCookie" name="emailCookie"/>
<c:set var="emailC" value="emailCookie.value" scope="request" />
<html:text name="logonForm" property="accountEmail" value='<c:out value="${requestScope.emailC}"/>' styleId="email" styleClass="txt"/>
# 4
>I was stupid enuf not to have imported the required JSTL tag for the same!!! :)...
> Dont know how it escaped my attention...!
Its the sort of mistake you can never spot yourself. I've done it myself many times.
With regards to this new error, you can never nest a custom tag as an attribute to another custom tag. But there are alternatives.
What server are you using?
Is it a JSP2.0 container? Is your web.xml declared as version 2.4?
If you can use JSP2.0, then you can use EL directly with the struts tags:
<html:text name="logonForm" property="accountEmail" value="${requestScope.emailC}" styleId="email" styleClass="txt"/>
Option #2:
Nest the tag within html:text to set the value.
<c:set var="emailC" value="emailCookie.value" scope="request" />
<html:text name="logonForm" property="accountEmail" styleId="email" styleClass="txt">
<c:out value="${requestScope.emailC}"/>
</html:text>
option #3: populate the bean in the action that is loading the page. then you don't need a value tag in your page because it just displays the value in the formbean already.