JSP - javabean 返回 null,但为什么呢?
当用户按下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能您没有让 servlet 在会话范围内存储 bean。但既然您无论如何都在使用 servlet,请忘记
的事情。这在技术上毫无价值。它还将模型与视图紧密耦合。只需让 servlet 将 bean 放入会话范围即可:
然后在用户主页中,只需使用 EL(表达式语言)来访问
User
bean。${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:
Then in the user home page, just use EL (Expression Language) to access the
User
bean.The
${foo}
basically searches for an attribute which is stored with the namefoo
in page, request, session and application scopes and returns the first non-null value. In this case, the${user}
will refer to yourUser
bean which you've stored in the session scope. The${user.username}
will basically display the return value ofuser.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 usecom.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.