25
Aug
10

Removing JSF Managed Bean from the Session


This page describes the process of cleaning up the session after using a JSF managed bean.

Background

JSF managed beans can be request or session scoped. If they are session scoped then they must be cleaned up manually after the user has navigated off the page. Otherwise they will hang around until the session times out.

In order to do a clean up just execute the following when the user performs the last step on the page.

  1. Program a cleanup() method in the managed bean (see below)
  2. Have the cleanup() method called by the last step the user takes on the page.
  3. Setup a nav-rule that allows the user to navigate to a new page.
	public String cleanup() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
		.getExternalContext().getSession(true);
		session.removeAttribute("page1MB");

		return "page2";
	}

The clean up method is called by a command button:

<h:commandButton value="cleanup" action="#{page1MB.cleanup}"/>

A nav rule that directs the user to another page:

	<navigation-rule>
		<from-view-id>/page1.jsp</from-view-id>
		<navigation-case>
			<from-outcome>page2</from-outcome>
			<to-view-id>/page2.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>

Use a SnoopServlet to verify that the values have been removed:
https://numberformat.wordpress.com/2010/01/02/snoopservlet-example/

Advertisements

0 Responses to “Removing JSF Managed Bean from the Session”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 74 other followers

August 2010
S M T W T F S
« Jun   Sep »
1234567
891011121314
15161718192021
22232425262728
293031  

Blog Stats

  • 801,304 hits

%d bloggers like this: