• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting session attribute inside custom tag

 
Mark Brothers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you set a session attribute inside a custom tag, so that the attribute can be retrieved by the next JSP page invoked?

Custom tag in JSP:
<a href="<osca:sendEmail juvenile="r.juvData" part="part"/>"><bean:write name="part" property="contactEmail"/></a>

Custom tag code snippet:
public int doStartTag() throws JspException {

StringBuffer results = new StringBuffer();
JuvenileData jData = (JuvenileData)pageContext.findAttribute(juvenile);
JuvenileParticipationData pData = (JuvenileParticipationData)pageContext.findAttribute(name);
UserData userData = (UserData)pageContext.findAttribute(user);

//this gets the program name for the entire participation collection
results.append(pData.getProgramName());

// Print the results to our output writer
ResponseUtils.write(pageContext, results.toString());

// Continue processing this page
return SKIP_BODY;
}

I would like to set the value of pData.getProgramName() onto the session (or somewhere) so I can then put in my next JSP page the following (or something similar):
<%=session.getAttribute("programname")%>

Any help would be greatly appreciated. Thanks!
[ August 09, 2004: Message edited by: Mark Brothers ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34689
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
You can get access to the session through the pageContext:
 
Mark Brothers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added the code in bold below to my custom tag:
public int doStartTag(HttpServletRequest request) throws JspException {

StringBuffer results = new StringBuffer();
JuvenileData jData = (JuvenileData)pageContext.findAttribute(juvenile);
JuvenileParticipationData pData = (JuvenileParticipationData)pageContext.findAttribute(name);
UserData userData = (UserData)pageContext.findAttribute(user);

HttpSession session = pageContext.getSession();
session.setAttribute("program", (pData.getProgramName()));


results.append(pData.getProgramName());

ResponseUtils.write(pageContext, results.toString());

return SKIP_BODY;
}

In my next JSP, I have the line:
<TD><%=session.getAttribute("program")%></TD>

On my JSP page, I get null, instead of the value from the pData.getProgramName

Is it due to the setAttribute being on one JSP page and the getAttribute being on a separate JSP page that gets called fromt he custom tag? Thanks for all the help and assistance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34689
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
I would expect that to work. Try outputing the session attribute's value at different points in time to see where it is disappearing. Places to try include: later in the custom tag, the first JSP and the second JSP. Also, is it possible the session value is getting overlaid at some point?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic