Neo Cortex
Neo Cortex

Reputation: 47

How to redirect jsp

I have LoginServlet like this:

package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

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

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username.equals("gogikole") && password.equals("1234")) {
            response.sendRedirect("mainMenu.jsp");
            return;
        }
    }
}

And login.jsp like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>
</head>
<body>
<form method="post" action="LoginServlet"></form>
    <table>
        <tr>
            <td>User name</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Login"></td>
        </tr>
    </table>
</body>
</html>

For some reason when I input correct username: (gogikole) and password: (1234) and click on submit button it won't to redirect me to mainMenu.jsp

When I click on button, nothing happen it just stay on login page like I didn't even click or type anything.

How to fix that problem and redirect when I click on submit button (Login)?

I created mainMenu.jsp and, for now that is one allmost blank page only with message "Welcome".

Upvotes: 0

Views: 39

Answers (1)

Sagar Pudi
Sagar Pudi

Reputation: 4824

Your submit button is not submitting the form. i.e., you have closed the form immediately:

<form method="post" action="LoginServlet"></form>

The problem is that your form inputs and submit button are outside the form You have to enclose as below.

<form method="post" action="LoginServlet">
    <table>
        <tr>
            <td>User name</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Login"></td>
        </tr>
    </table>
 </form>

Upvotes: 1

Related Questions