0

I have a very basic login servlet

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // get request parameters for userID and password
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username.equals("marti") && password.equals("bosch")) {
            response.sendRedirect("login_success.jsp");
        } else {
            RequestDispatcher rd = getServletContext().getRequestDispatcher(
                    "/login.html");
            PrintWriter out = response.getWriter();
            out.println("<font color=red>Either user name or password is wrong.</font>");
            rd.include(request, response);
        }
    }
}

When I call the servlet it from the login.html:

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
    <form action="LoginServlet" method="post">
        <p>Username: <input type="text" name="username"> </p>
        <p>Password: <input type="password" name="password"> </p>
        <p><input type="submit" value="Login"></p>
    </form>
</body>
</html>

if I input the correct user name and password, the browser correctly renders login_success.jsp. However, when I do not enter the right user name and password, the browser shows the unrendered html as plain text:

<font color=red>Either user name or password is wrong.</font>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
    <form action="LoginServlet" method="post">
        <p>Username: <input type="text" name="username"> </p>
        <p>Password: <input type="password" name="password"> </p>
         <p><input type="submit" value="Login"></p>
    </form>
</body>
</html>

How can I make the LoginServlet place the message under in the right place (after the body tag)? Thanks

Martí Bosch
  • 329
  • 2
  • 12

1 Answers1

2

Exactly, you need to put it as a content of an HTML document:

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
    <font color=red>Either user name or password is wrong.</font>
    <form action="LoginServlet" method="post">
        <p>Username: <input type="text" name="username"> </p>
        <p>Password: <input type="password" name="password"> </p>
         <p><input type="submit" value="Login"></p>
    </form>
</body>
</html>

Additionally, a <font> tag is deprecated in HTML5.

EDIT:

So either you need to sendRedirect to the page where an error message is included:

response.sendRedirect("login_error.jsp");  //containing exactly the same html I posted

Or modify your form JSP, so it renders an additional error message after the authentication failure. You can accomplish that by passing a parameter from a Servlet to JSP. Refer to this thread.

Community
  • 1
  • 1
Kuba Rakoczy
  • 3,954
  • 2
  • 15
  • 16
  • yes, of course, but i mean how can i place this message from the LoginServlet. Concerning the font tag, I just copied the code from a tutorial, when I understand more about Java servlets I'll use right HTML5. Thank you anyway – Martí Bosch Dec 11 '14 at 12:23
  • Fixed. I misread your question for the first time, apologies :) – Kuba Rakoczy Dec 11 '14 at 12:37
  • well, from this I guess that there is no way to add some string to a specific position using the out.println() method, which is what I wanted to know. Thanks! – Martí Bosch Dec 11 '14 at 14:31