【问题标题】:Getting session sub-attributes from object从对象获取会话子属性
【发布时间】:2017-01-17 19:40:45
【问题描述】:

我目前有一个用户对象:

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;
}

还有一个登录函数,它返回一个用户对象:

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。

提前致谢。

【问题讨论】:

    标签: java servlets


    【解决方案1】:

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

    这是一个例子:

    request.getSession().setAtribute("user",tempUser);

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

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

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

    还有其他格式,你可以探索一下

    看看这篇文章

    How to use session in jsp pages to get information?

    您也可以使用 scriptlet,但它们太陈旧了。

    只需尝试使用 EL 或切换到使用弹簧架,您可以随心所欲地处理所有事情。

    【讨论】:

    • 兄弟....我感激不尽。那里的工作真的很棒。感谢您,我设法通过这样做来解决它: User tempUser = (User)session.getAttribute("user"); out.print(tempUser.username);再次,非常感谢兄弟,真的。
    • 好的,很好。尝试使用 EL,因为它现在被广泛使用,如果可能的话尝试切换到 Restful webservices
    • @WilliamPfaffe 如果将 User 类存储在会话中,则存在潜在的安全风险。填充后,密码将始终可用。用户登录后,将不再需要密码。考虑从 User 类中删除密码属性,或者在用户登录后至少将其设为空。
    • @Andrew 是的,这是一个需要注意的好点,是的,我们可以使用单独的 DAO 和 DTO 层来解决这些问题。有空的时候看看那些网站javatpoint.com/q/2615
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2023-03-26
    • 2023-03-09
    • 2014-08-05
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多