Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6764003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:32:36+00:00 2026-05-26T14:32:36+00:00

Continuing on my previous question , I’m trying to initialize a session-scoped JSF bean

  • 0

Continuing on my previous question, I’m trying to initialize a session-scoped JSF bean when the application’s session first starts, so the bean will be available to a user, regardless of which page they access on my web application first. My custom listener:

public class MyHttpSessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                .get("mySessionBean") == null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                    .put("mySessionBean", new MySessionBean());
        }
    }
}

However, this is giving me a stack overflow error. It appears that the put() method in the SessionMap class tries to create a new HttpSession, thus causing an infinite loop to occur with my listener. How can I initialize a JSF session-scoped bean when my application’s session first starts, without running into this issue?

I’m using JSF 2 with Spring 3, running on WebSphere 7.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:32:36+00:00Added an answer on May 26, 2026 at 2:32 pm

    The session isn’t been fully finished creating at that point. Only when the listener method leaves, the session is put into the context and available by request.getSession() as JSF’s getSessionMap() is using under the covers.

    Instead, you should be grabbing the session from the event argument and use its setAttribute() method. JSF lookups and stores session scoped managed beans just there and won’t create a new one if already present.

    public void sessionCreated(HttpSessionEvent event) {
        event.getSession().setAttribute("mySessionBean", new MySessionBean());
    }
    

    Note that I removed the superfluous nullcheck as it’s at that point impossible that the session bean is already there.


    Unrelated to the concrete problem, you should actually never rely on the FacesContext being present in an implementation which isn’t managed by JSF. It is quite possible that the session can be created during a non-JSF request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing from my previous question , is there a comprehensive document that lists all
I am continuing on from a previous question relating to loading instances of plugins
Continuing from this question : When I am trying to do fopen on Windows,
Continuing a previous question I asked here , I now need to move to
Continuing from a previous question , I keep searching for the optimal way to
Continuing my previous question . XML <albums> <album title=new_zealand> <album title=auckland> <image title=mt_eden_railroad_station/> </album>
Continuing my previous question Why I cannot derive from long? I found an interesting
Having wrestled with NSPopUpButton in a previous question here I am now trying to
I am continuing from my previous question I need a way to export my
Continuing from my previous question which was solved by @Oleg. The solution seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.