0

Setting cookies from SessionInit

asked 2012-08-14 14:31:01 +0800

RichardL gravatar image RichardL
768 4

Hi all,
I want to set cookies at the start of a user's session. From the init method of a class implementing SessionInit, I call the following method:

public static void setCookie(String name, String value, int days) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(60 * 60 * 24 * days);
		((HttpServletResponse) Executions.getCurrent().getNativeResponse())
				.addCookie(cookie);
	}

But I'm getting a NullPointerException at: Executions.getCurrent()....

Is it possible to set cookies from SessionInit?

Thanks,
Richard

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2012-08-14 16:01:51 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

You can use DesktopInit instead, and check for the cookie's existence to avoid setting it on every desktop creation. Executions.getCurrent() works in DesktopInit, so your code will work fine.

Since you are probably always creating a desktop when creating a session, it shouldn't matter whether you use SessionInit or DesktopInit.

link publish delete flag offensive edit

answered 2012-08-15 01:15:34 +0800

RichardL gravatar image RichardL
768 4

Thanks, gekkio!

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-08-14 14:31:01 +0800

Seen: 114 times

Last updated: Aug 15 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More