0

I have project maven web-application. I have a few JSP files with HTML code and JSP tags. I have a connection in the local database and a few servlets.

Problem is that when I logged in to the app, I want to print a welcome message for the logged user. This is the tag which should display a welcome message:

<div class="hello-text"><h1>Hello <span>${sessionScope.user_name}</span>. This is yours stats:</h1></div>

When I logged in the only text that I have is "Hello ${sessionScope.user_name}. This is your stats:

This is my servlet code for logging in:

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {

private static final long serialVersionUID = 2717450811223035557L;

private UserRepository userRepository = new UserRepositoryBean();

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setContentType("text/html");
    String login = req.getParameter("login");
    String password = req.getParameter("password");
    PrintWriter writer = resp.getWriter();

    if (login == null | login.isEmpty() | password == null | password.isEmpty()) {
        writer.write("ERROR");
        return;
    } else {
        if (userRepository.validateUser(login, password)) {
            HttpSession session = req.getSession();
            session.setAttribute("user_name", login);
            resp.sendRedirect("profile.jsp");
        } else {
            req.setAttribute("error", "Invalid login or password. Try again.");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }

    writer.close();
}

}

Why I don't have a message, for example, Hello Admin. This is your stats:? I always have Hello ${sessionScope.user_name}. This is your stats:...

  • Make sure you declare the correct version of the servlet spec in order to enable Expression Language https://stackoverflow.com/questions/30080810/el-expressions-not-evaluated-in-jsp – Thilo Feb 14 '19 at 11:30

1 Answers1

1

A sendRedirect should not be mixed by other output of some page.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String login = req.getParameter("login");
    String password = req.getParameter("password");

    if (login.isEmpty() || password.isEmpty()) {
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        writer.write("ERROR");
        return;
    }
    if (userRepository.validateUser(login, password)) {
        HttpSession session = req.getSession();
        session.setAttribute("user_name", login);
        resp.sendRedirect("profile.jsp");
    } else {
        req.setAttribute("error", "Invalid login or password. Try again.");
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }
}

The first if could be done by the validation - as empty input happens often - by a nice error message reposted to the same form with already done input saved.

If the JSP comes as HTML, then ensure it has a valid servlet mapping. Also the JSP better should not be placed in a public directory, but maybe under WEB-INF/jsps/ or such.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138