Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Web应用程序会话变量_Java_Session - Fatal编程技术网

Java Web应用程序会话变量

Java Web应用程序会话变量,java,session,Java,Session,当前用户登录后,如何将其信息存储在会话变量中?这些数据需要在登录后通过web应用程序访问 这就是我到目前为止所做的: User.java @Named @Table @Entity @SessionScoped public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationTy

当前用户登录后,如何将其信息存储在会话变量中?这些数据需要在登录后通过web应用程序访问

这就是我到目前为止所做的:

User.java

@Named
@Table
@Entity
@SessionScoped
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private long id;

    @Column(name = "firstname")
    private String firstname;

    @Column(name = "lastname")
    private String lastname;

    @Column(name="username")
    private String username;

    @Column(name = "password")
    private String password;

    public User() {}
...
@Named
@RequestScoped
public class UserData implements Serializable {

    private static final long serialVersionUID = 1L;
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    Transaction tx = null;
    List<User> users;
    String username;
    String password;

    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public UserData() {
        try {
            this.session = sessionFactory.openSession();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

public void login() {
    System.out.println("Authorize: Username: " + this.username + " Password: " + this.password);

    Query query = session.createQuery("from User U where U.username = :username and U.password = :password");
    query.setParameter("username", this.username);
    query.setParameter("password", this.password);
    System.out.println(query.list());
}
UserData.java

@Named
@Table
@Entity
@SessionScoped
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private long id;

    @Column(name = "firstname")
    private String firstname;

    @Column(name = "lastname")
    private String lastname;

    @Column(name="username")
    private String username;

    @Column(name = "password")
    private String password;

    public User() {}
...
@Named
@RequestScoped
public class UserData implements Serializable {

    private static final long serialVersionUID = 1L;
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    Transaction tx = null;
    List<User> users;
    String username;
    String password;

    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public UserData() {
        try {
            this.session = sessionFactory.openSession();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

public void login() {
    System.out.println("Authorize: Username: " + this.username + " Password: " + this.password);

    Query query = session.createQuery("from User U where U.username = :username and U.password = :password");
    query.setParameter("username", this.username);
    query.setParameter("password", this.password);
    System.out.println(query.list());
}
@Named
@请求范围
公共类UserData实现可序列化{
私有静态最终长serialVersionUID=1L;
SessionFactory SessionFactory=HibernateUtil.getSessionFactory();
会话=空;
事务tx=null;
列出用户名单;
字符串用户名;
字符串密码;
公共字符串getUsername(){
返回用户名;
}
公共字符串getPassword(){
返回密码;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共用户数据(){
试一试{
this.session=sessionFactory.openSession();
}
捕获(例外e){
e、 printStackTrace();
}
}
公共无效登录(){
System.out.println(“授权:用户名:+this.Username+”密码:+this.Password”);
Query Query=session.createQuery(“来自用户U,其中U.username=:username和U.password=:password”);
query.setParameter(“用户名”,this.username);
query.setParameter(“密码”,this.password);
System.out.println(query.list());
}
返回了正确的用户对象,但我不知道如何在用户的会话数据中保存用户对象

我正在使用的技术(如果有帮助):

  • 冬眠
  • MySQL
  • JPA
  • JSF
  • 野蝇

由于您的
用户
类由
@命名的
注释管理,因此您只需将当前用户的实例注入您想要使用的类中,即可访问该用户:

@Inject
private User user;
另一种解决方案是使用如下代码:

FacesContext context = FacesContext.getCurrentInstance();
User user = context.getApplication().evaluateExpressionGet(context, "#{user}", User.class);

您使用的是哪个web框架?@redflar3我没有使用web框架,这是一个普通的javaEE。请您解释一下使用
evaluateExpressionGet(…)
还是使用
context.getAttributes().get(“user”)