JSP - javabean 返回 null,但为什么呢?

发布于 2024-10-18 10:01:54 字数 1170 浏览 1 评论 0原文

当用户按下 JSP 上的登录按钮时,请求将提交到我的 servlet,该 servlet 通过 getParameter() 从登录表单获取输入的用户名和密码。

用户名和密码存储在 javabean 中。如果我放置一个 println() 语句来显示 bean.getUsername()bean.getPassword() 的值,那么它会打印日志中的用户名和密码。

但我的母版页上的以下代码片段

<jsp:useBean id="bean" class="package.Bean" scope="session" />
<jsp:getProperty name="bean" property="username" />

显示 null 而不是用户名。我做错了什么?

protected void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException
{
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    // Store username and password in bean from login.jsp
    Bean bean = new bean();
    bean.setUsername(username);
    bean.setPassword(password);

    // check login details with database

       if (login success)
    {   
        HttpSession session = request.getSession();
        session.setAttribute("username", username);

        // redirect user to welcome page
    }
    else
        // login failed
}

When an user presses the login button on a JSP, the request is submitted to my servlet which gets the entered username and password from the login form by getParameter().

The username and password are stored in a javabean. If I put a println() statement which shows the values of bean.getUsername() and bean.getPassword(), then it prints the username and password in the log.

But the following code snippet on my master page

<jsp:useBean id="bean" class="package.Bean" scope="session" />
<jsp:getProperty name="bean" property="username" />

shows null instead of the username. What am I doing wrong?

protected void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException
{
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    // Store username and password in bean from login.jsp
    Bean bean = new bean();
    bean.setUsername(username);
    bean.setPassword(password);

    // check login details with database

       if (login success)
    {   
        HttpSession session = request.getSession();
        session.setAttribute("username", username);

        // redirect user to welcome page
    }
    else
        // login failed
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

廻憶裏菂餘溫 2024-10-25 10:01:54

可能您没有让 servlet 在会话范围内存储 bean。但既然您无论如何都在使用 servlet,请忘记 的事情。这在技术上毫无价值。它还将模型与视图紧密耦合。

只需让 servlet 将 bean 放入会话范围即可:

User user = userDAO.find(username, password);
if (user != null) {
    request.getSession().setAttribute("user", user); // Store logged-in user in session.
    response.sendRedirect("home"); // Redirect to login home page or whatever.
} else {
    request.setAttribute("message", "Unknown login, please try again"); // Store error message for redisplay in login page.
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to login page.
}

然后在用户主页中,只需使用 EL(表达式语言)来访问 User bean。

<p>Welcome, <c:out value="${user.username}" /></p>

${foo} 基本上搜索在页面、请求、会话和应用程序范围中以名称 foo 存储的属性,并返回第一个非空值。在这种情况下,${user} 将引用您存储在会话范围中的 User bean。 ${user.username} 基本上会显示 user.getUsername() 的返回值。

顺便说一下, 并不是必需的,但它可以防止您的页面受到潜在的 XSS 攻击。

另请参阅:


与具体问题无关,package 是一个关键字,因此是一个非法的包名称。发布过于简单的代码示例时请小心;)如果您想混淆包名称,请使用 com.example 或其他内容。


更新:根据您的更新,您在会话范围中设置用户名,而不是 bean 本身。尽管您可以通过 ${username} 显示它,但为了能够使用 ${bean.username},您需要相应地修复它,如下所示上面的答案中有描述。

Likely you haven't let the servlet store the bean in the session scope. But since you're using servlets anyway, forget the <jsp:useBean> thing. It's technically worthless. It also tight-couples the model with the view.

Just let the servlet put the bean in session scope:

User user = userDAO.find(username, password);
if (user != null) {
    request.getSession().setAttribute("user", user); // Store logged-in user in session.
    response.sendRedirect("home"); // Redirect to login home page or whatever.
} else {
    request.setAttribute("message", "Unknown login, please try again"); // Store error message for redisplay in login page.
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to login page.
}

Then in the user home page, just use EL (Expression Language) to access the User bean.

<p>Welcome, <c:out value="${user.username}" /></p>

The ${foo} basically searches for an attribute which is stored with the name foo in page, request, session and application scopes and returns the first non-null value. In this case, the ${user} will refer to your User bean which you've stored in the session scope. The ${user.username} will basically display the return value of user.getUsername().

The <c:out> is by the way not necessary, but it prevents your page from potential XSS attacks.

See also:


Unrelated to the concrete problem, package is a keyword and thus an illegal package name. Please take care when posting oversimplified code examples ;) Rather use com.example or something if you'd like to obfuscate package names.


Update: as per your update, you're setting the username in the session scope, not the bean itself. Even though you would be able to display it by ${username}, in order to be able to use ${bean.username}, you need to fix it accordingly as described in above answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文