Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cookies

 
chris locke
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your action can implement ServletRequestAware to get access to the HttpServletRequest object. From that you can get the HttpSession object...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic