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 8019057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:16:31+00:00 2026-06-04T21:16:31+00:00

I am using a session scoped managed bean for handling login in a Java

  • 0

I am using a session scoped managed bean for handling login in a Java EE application. After I authenticate the user, the user object is saved in this session bean. However, after I refresh the page, the session bean values are gone.

I was debugging the code and it results that the constructor of the session scoped managed bean is called again on page refresh, therefore initializing the user object with a new user. I guess this is not a normal behavior since it should be preserved on the session shouldn’t it?

I am posting some parts of the login managed bean including the parameters and the login method. Basically the enteredEmail and enteredPassword stand for the entered data on the login form. If the authentication succeeds, the loggedIn boolean is turned to true and the logged in user object is stored in the checkedUser variable.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class LoginController  implements Serializable {

@EJB
private LoginSessionBean loginSessionBean;
@EJB
private LecturerFacade lecturerFacade;

private Lecturer checkedUser;
private String enteredEmail;
private String enteredPassword;
private boolean loggedIn;





/** Creates a new instance of loginController */
public LoginController() {
   loggedIn = false;
   checkedUser = new Lecturer();
}
public String login(){
    RequestContext context = RequestContext.getCurrentInstance();
    FacesMessage msg = null;

    this.setCheckedUser(lecturerFacade.findLecturerByEmail(enteredEmail));

    if(loginSessionBean.checkPassword(checkedUser, enteredPassword))
    {
        loggedIn = true;
        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", checkedUser.getFirstName()+ " " + checkedUser.getLastName());



        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);



    }
    return "Index";

I am also posting the two EJBs that the above managed bean uses. The lecturerFacade retrieves the user object with the entered email, while the loginSessionBean checks the password.

@Stateless
public class LecturerFacade extends AbstractFacade<Lecturer> {
@PersistenceContext(unitName = "EffectinetWebPU")
private EntityManager em;

Logger logger = Logger.getLogger("MyLog");
FileHandler fh;


protected EntityManager getEntityManager() {
    return em;
}

public LecturerFacade() {
    super(Lecturer.class);
}

public Lecturer findLecturerByEmail(String email) {
    try {
        return (Lecturer) this.getEntityManager().createQuery("SELECT l FROM Lecturer l WHERE l.email = :email").setParameter("email", email).getSingleResult();
    } catch (NoResultException e) {
        System.err.println("Caught NOResultException: "+  e.getMessage());
        return null;
    } catch (NonUniqueResultException e) {
        System.err.println("Caught NonUniqueResultException: "+  e.getMessage());
        return null;
    } catch (IllegalStateException e) {
        System.err.println("Caught IllegalStateException: "+  e.getMessage());
        return null;
    }
}

_

@Stateless

public class LoginSessionBean {

// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@PersistenceContext(unitName = "EffectinetWebPU")
private EntityManager em;

protected EntityManager getEntityManager() {
    return em;
}

public void setEntityManager(EntityManager em) {
    this.em = em;
}



public boolean checkPassword(Lecturer user, final String enteredPassword) {
    if (user.getPassword().equals(enteredPassword)) {
        return true;
    } else {
        return false;
    }
}

}

Please if someone has any suggestion of what is going wrong, please tell me

Im using glassfish 3.1 as application server and Primefaces as JSF library. Also, I have checked and the imported the sessionScoped annotation from the right package and not from javax.enterprise…

  • 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-06-04T21:16:32+00:00Added an answer on June 4, 2026 at 9:16 pm

    Your problem is thus here:

    <p:menuitem value="Logout" ... onclick="#{loginController.logout()}"/>
    

    The onclick attribute should represent a JavaScript handler function which is to be executed in the webbrowser when the enduser clicks the element. Something like

    onclick="alert('You have clicked this element!')"
    

    The onclick attribute also accepts a ValueExpression, so you can even let JSF/EL autogenerate its value accordingly:

    onclick="#{bean.onclickFunction}"
    

    with

    public String getOnclickFunction() {
        return "alert('You have clicked this element!')";
    }
    

    All the EL is thus evaluated when the page is rendered. In your particular case, the logout() method is called everytime the EL is evaluated and thus you’re invalidating the session everytime the page is rendered!

    You need to bind it to an attribute which takes a MethodExpression like <h:commandLink action>, <h:commandButton action> and in this particular case <p:menuitem action>.

    <p:menuitem value="Logout" ... action="#{loginController.logout()}"/>
    

    This can be understood by understanding basic HTML and JavaScript concepts and keeping in mind that JSF ultimately produces HTML/CSS/JS. Open the JSF page in webbrowser, rightclick and View Source to realize it.

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

Sidebar

Related Questions

I have a session scoped managed bean with name 'usermanager'. i want to store
So, I've seen this post: JSF - session scoped bean shared by browsers on
I'm looking for best way of using session within zf application. At first I
I'm trying to make this 2-player web game application using Spring MVC. I have
I want to ask if i place my managed bean in session scope, then
Background: I have a JSF 2.0 application using MyFaces. Using this application, the users
I am trying to access a object using session scope . I am doing
public MyClass(int someUniqueID) { using(//Session logic) { var databaseVersionOfMyClass = session.CreateCriteria(/*criteria*/) .UniqueResult<MyClass>(); //Load logic
I have been using SESSION Variables to enter data with loginName as way to
I have a question about using session, or maybe TempData to store data while

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.