This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes Cookies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Cookies" Watch "Cookies" New topic


chris locke

Joined: Aug 13, 2010
Posts: 17
Hi gurus,

With basic servlets I can usually get away with the code:

HttpSession session = request.getSession();
if (session.isNew()) {

When throwing struts2 into the mix, I see various ways to add cookies. What is the best way to do this? ie. What is considered best practice? Should I be implementing cookiesAware? Some have suggested Struts2 isn't very good with cookies, so it's better not to use it at all? Others have suggested the use of "ActionContext.getContext().getSession()" to Map your new cookie?

Thanks in advance
Ankit Garg

Joined: Aug 03, 2008
Posts: 9293

Your action can implement ServletRequestAware to get access to the HttpServletRequest object. From that you can get the HttpSession object...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton

Joined: Sep 29, 2008
Posts: 12617

How is S2 "not very good with cookies"? What would that even mean? What does the session have to do with it; cookies are in the request/response, no?

CookiesAware is the appropriate way to get access to cookies.
I agree. Here's the link:
subject: Cookies