JSP-javabean返回null,但是为什么呢? [英] JSP - javabean returns null, but why?

查看:83
本文介绍了JSP-javabean返回null,但是为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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().

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

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" />

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

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
}

推荐答案

可能您没有让servlet在会话范围内存储Bean.但是由于无论如何都在使用servlet,因此请忘记<jsp:useBean>.从技术上讲,它毫无价值.还将模型与视图紧密耦合.

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.

只需让servlet将bean置于会话范围内即可:

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

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

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

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

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

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().

<c:out>并不是必需的,但是它可以防止您的页面受到潜在的XSS攻击.

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

  • Our JSP wiki page
  • Our Servlets wiki page

与具体问题无关,package是关键字,因此是非法的软件包名称.发布过分简化的代码示例时请多加注意;)如果您想混淆软件包名称,请使用com.example或其他名称.

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.

更新:根据您的更新,您是在会话作用域中设置用户名,而不是bean本身.即使您可以通过${username}显示它,但是为了能够使用${bean.username},也需要按照上述答案中的说明进行相应的修复.

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.

这篇关于JSP-javabean返回null,但是为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆