Reputation: 2349
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
}
Upvotes: 1
Views: 4032
Reputation: 1109542
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.
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.
Upvotes: 2