aspose file tools*
The moose likes Struts and the fly likes Setting session attribute inside custom tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Setting session attribute inside custom tag" Watch "Setting session attribute inside custom tag" New topic
Author

Setting session attribute inside custom tag

Mark Brothers
Greenhorn

Joined: Apr 15, 2004
Posts: 14
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

Joined: May 26, 2003
Posts: 31074
    
232

Mark,
You can get access to the session through the pageContext:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mark Brothers
Greenhorn

Joined: Apr 15, 2004
Posts: 14
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

Joined: May 26, 2003
Posts: 31074
    
232

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting session attribute inside custom tag