File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 9462

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
It's not a secret anymore!