从对象获取会话子属性

时间:2016-09-09 15:40:17

标签: java servlets

我目前有一个User对象:

public class User {

public final String username, password, email;
public final double balance;

public User(String username, String password, String email, double balance) {
    this.username = username;
    this.password = password;
    this.email = email;
    this.balance = balance;
}

一个登录函数,它返回一个User对象:

public User checkLogin(String usrname, String pssword) {
        Front fr = new Front();
        User newUser = null;
        try {
            int count = 0;
            Class.forName("com.mysql.jdbc.Driver");   
            Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/projects", "william", "william");
            java.sql.Statement mySt = myConn.createStatement();
            String rs = "SELECT username, password, email, balance FROM user WHERE username='" + usrname + "'AND password='" + pssword + "'";

    System.out.println(rs);
    ResultSet myRS = mySt.executeQuery(rs);
    while (myRS.next()) {
        newUser = new User(myRS.getString("username"),myRS.getString("password"),myRS.getString("email"),myRS.getDouble("balance"));


    count++;
    }

    if (count == 0) {
        return newUser;
    }

    if (count == 1) {
        return newUser;



    }
    myConn.close();
} catch (SQLException | HeadlessException ex) {
    JOptionPane.showMessageDialog(null, ex);
} catch (ClassNotFoundException ex) {
    Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return newUser;

}

最后,一个servlet,它处理表单,并设置不同的属性:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    Double balance = 0.00;
    String origin = request.getParameter("origin");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    switch(origin){
    case "login":
    db.checkLogin(username, password);
    User tempUser;
        if((tempUser = db.checkLogin(username, password)) != null){
            out.print("You will be redirected");
            request.getSession().setAttribute("user", tempUser);
            response.sendRedirect("index.jsp");
        }else if(db.checkLogin(username,password) == null){
            out.print("Wrong login info, please try again!");
            response.sendRedirect("login.jsp");
        }

    break;
    case "register":
        db.registerUser(username, password, balance, email);
        response.sendRedirect("login.jsp");
    break;
}

}

我想要做的是,例如,在网页上显示,用户拥有的子值之一(用户名,电子邮件等)。但是,我不确定如何使用session.getAttribute,以及如何获取tempUser.username。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以将该属性添加到请求中,如果要将其返回到视图页面,则可以从返回到视图的响应对象访问该属性。我将很快提供一个例子。

以下是一个例子:

request.getSession()setAtribute( “用户”,tempUser);

并在index.jsp上的视图页面中,如您的示例所示,您可以使用这些格式来实现

强烈建议使用EL(表达式语言)和JSTL标记。例如,在这里你可以使用EL作为

<td>Username: </td>
<td><input type="text" value="${user.username}" /></td>

还有其他格式,你可以探索它们

看一下这篇文章

How to use session in jsp pages to get information?

你也可以使用scriptlet,但它们太过obesolete了。

只需尝试使用EL或切换到使用弹簧支架,您可以根据需要有效地处理所有内容。