Apache Wicket > Framework Documentation > Reference library > How to do things in Wicket > View Layer > Logging Out- Invalidating Session and Redirect
Added by Will Hoover, last edited by Will Hoover on Feb 26, 2008  (view change)

Add the following to your corresponding WebPage...

...

Link logoutLink = new Link("my-link-wicket-id") {

	public void onClick() {
		getSession().invalidate();
		getRequestCycle().setRedirect(true);
		setResponsePage(WhereEverYouWantToRedirect.class); [1]
	}
};

...

You can also redirect before a page renders:

...

	@Override
	protected void onBeforeRender() {
		super.onBeforeRender();
		getRequestCycle().setRedirect(true);
		throw new RestartResponseException(WhereEverYouWantToRedirect.class);
	}

...

If you need to redirect to an external webpage...

...

Link logoutLink = new Link("my-link-wicket-id") {

	public void onClick() {
		getSession().invalidate();
		getRequestCycle().setRequestTarget(new RedirectRequestTarget("http://www.another-web-site.com") );
	}
};

...

[1] it is important to use setResponsePage() variant that takes a page class and not a page instance because a page instance would be stored in session that is now marked for invalidation - thus resulting in a page expired error