Meaningless Drivel is fun!*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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
 
Similar Threads
Unable to locate setter methof for attribute lock in the tag class
How to pass row id of colection to next JSP page for display using Struts
sess.existsAttribute tag not working?
help with custom tags
Capturing the element in collection that was chosen