how to get the session varibale value in JSF

Discussions

Web tier: servlets, JSP, Web frameworks: how to get the session varibale value in JSF

  1. Hi,

    I'm subramanian, i'm new for JSF framework. I need to know how we get the session variable value which we already set by using of bean property value in faces-config.xml file.

    In faces-config.xml we give the session for your bean...

    <managed-bean>
    <managed-bean-name>login</managed-bean-name>
    <managed-bean-class>LoginBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

    So in above example... all parameter are session scope which is resides in LoginBean.. right...

    U just assume... userId is the parameter... that is the session variable which is resides in LoginBean...

    now i need to get the value of userId from another JSP page..
    i try myself..

    Object ss=session.getAttribute("userId");
    Object vv=session.getValue("userId");
    String rr=request.getParameter("login.userId");

    but it's retrieve only null value...

    i don't know whether the session varible not set properly or i made mistake to retrieve the session varible...

    please help me.. it's very urgent..

    Regards,
    Subbus

    Threaded Messages (5)

  2. Hi,

    From what you are saying, it seems like you are receiving the userid from some jsp, i.e. an input that is coming from the user in a request.

    So to retrieve that you would do somethign simmilar to what you do in servlets.. i.e. from the HTTPServletRequest retreive the value for what the user has sent you in your LoginBean. And once you retreive the value set your userId in the LoginBean to whatever value you retreived and now it would be set in the session and you can retreive it anytime you want.. but you cant expect to retreive it if its not set first..

    Hope it helps..
  3. Seesion Variable in JSF[ Go to top ]

    Hi,

    Thanks for your response, Exactly correct what you are saying .. but if u r using JSF framework, u need to mention the scope of bean in faces-config.. so in that face-config.xml u have to mention the "session" is the scope for LoginBean(already i wrote the code).. now the session scope set to the LoginBean and all the parameters also... (for example userId)

    What i'm asking is.. how i get the session parameter...
    Could u please give any example for handling session in JSF framework

    Regards
    Subbus
  4. Seesion Variable in JSF[ Go to top ]

    There are two ways of doing this:
    1) The "Right" way
      FacesContext ctx = FacesContext.getCurrentInstance();
      Application app = ctx.getApplication();
      LoginBean lb = (LoginBean)app.createValueBinding("#{login}).getValue(ctx);

    2) Use getExternalContext() on the Faces context which will give you access to the request and session objects directly.
  5. Slight Typo[ Go to top ]

    Missed the closing quote from the first example but you get the idea.
  6. Session variable in jsf[ Go to top ]

    Thanks Duncan,

    But it's not working .. still it's showing some error.. could u please tell me where should i write that code in Logout jsp or backend bean with u r send to me..

    And one more doubt.. here u mention
    Application app=ctx.getApplication();....

    In above Application as u mention Application scope of our bean or that is any other class for handling session scope..

    if u have free time plz give example program for that..

    Thanking you
    Subbus