0

I am trying to access an ID used to log into my interface in another class for an SQL query.

I am currently using:

    public void login (String UName)
{
try 
    //sql stuff

            FacesContext context = FacesContext.getCurrentInstance();
    Map<String, Object> userdata = context.getExternalContext().getSessionMap();

        UserBean userBean = new UserBean();
        userBean.setUserID(rs.getString(1).toString());
        userBean.setMajor(rs.getString(5).toString());
        userdata.put("userBean", userBean);
    }

Which allows me to get the userID, and I can display that data on JSF webpage.

However, I want to get that ID in another java class, ie viewregistereduserID.java. So, for example, I want to get the UserID used to login as a string that can be used for other methods.

I have tried using UserBean userBean = (UserBean) userdata.get("userBean"); and @ManagedProperty(value="#{userBean}") but the result comes out as null both times when I tried userBean.getUserID.

I used @SessionScoped for both java classes in this example.

Does anyone know how I can acess the data in a string?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jni4
  • 27
  • 4

2 Answers2

0

notification:i am English learner now if there is any misunderstanding,please let me know

What i suppose you to do just create a other class for return the value. You can create a class call viewregistereduserID

public class viewregistereduserID{
//fields
public String userID;
//constructor
viewregistereduserID(String userID){
   this.userID=userID;
}
//method
public String getUserID(){
     return this.userID; 
 }

}

in this case ,you can call this class as static or create a object on any other class.

call it on other class: you can create a object:

public class login(String name){
   public static void main(String[] args){
          viewregistereduserID VID=new viewregistereduserID();
          System.out.print(VID.getUserID());
}

}

  • Hey thanks for the quick reply. Unfortunately your response didn't work out for me but your English seems good. :) – jni4 Sep 29 '16 at 05:43
0

I figured out the solution to my own question by:

    private UserBean userInfo;

    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, Object> m = context.getExternalContext().getSessionMap();
    userInfo = (UserBean) m.get("userBean");
    System.out.println(userInfo.getUserID());
jni4
  • 27
  • 4