0

It's my first day trying servlets. It's just a simple login function, but I get an error that doesn't explain what I'm doing wrong. It just says

404: The requested resource is not available.

Here's my servlet:

public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String key = "ole";
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        response.sendRedirect("NewFile.jsp");
        if (session.getAttribute(username).equals(key)
                && session.getAttribute(password).equals(key)) {
            response.sendRedirect("secret.jsp");
        } else {
            response.sendRedirect("NewFile.jsp");
        }
    }

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

}

And here's my login page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login page</title>
</head>
<body>

    Enter username and password below.
    <form method=get action="hej">
        <input type="text" user="username" /> <input type="password"
            user="password" /> <input type="submit" value="Login" />
    </form>
</body>
</html>

There is a file called secret.jsp as well, but there's nothing interesting in there. I don't understand why it's returning 404. When I have remove the if statement it works... only then, there's not much function.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
Rasmus
  • 241
  • 3
  • 19

3 Answers3

2

As you say it works without IF-ELSE then I guess your web.xml entry is correct.

First change this to

<input type="password" user="password" />

this

<input type="password" name="password" />

input tag does not have any user attribute , it's name.

Next mistake is , you are using session.getAttribute to access parameters that you have not set, yet.

Your code should be

if (username.equals(key)
        && password.equals(key)) {
    response.sendRedirect("secret.jsp");
} else {
    response.sendRedirect("NewFile.jsp");
}

session.getAttribute can only be used when you have in your code session.setAttribute example session.setAttribute("username","ole");session.setAttribute("password","ole");.

Then only you could get a value with session.getAttribute("username") OR session.getAttribute("password"). But right now it only returns NULL. So code won`t work.

You could add this code in your existing code and it will work.

session.setAttribute(username,username);
session.setAttribute(password,password);

Your complete code should be

String username = request.getParameter("username");
String password = request.getParameter("password");
session.setAttribute(username,username);
session.setAttribute(password,password);

//response.sendRedirect("NewFile.jsp");

if (session.getAttribute(username).equals(key)
        && session.getAttribute(password).equals(key)) {
    response.sendRedirect("secret.jsp");
} else {
    response.sendRedirect("NewFile.jsp");
}
Anurag Anand
  • 500
  • 1
  • 7
  • 13
  • I tried it out, I learned a lot from the post, however I still get a NullPointerException at the if. I also dont understand why it's not .setAttribute("username", username); ... you take the parameter from the .jsp file, yes? – Rasmus Nov 14 '14 at 11:59
  • @Rasmus why it's not .setAttribute("username", username); because you are accessing the attributes as session.getAttribute(username) not session.getAttribute("username"). For getting NULL at IF , either the value returned by getAttribute is NULL OR the attribute is not present. Can you provide your updated code – Anurag Anand Nov 16 '14 at 04:04
  • @Rasmus You can use session.getAttribute("username") if you set the attributes like this session.setAttribute("username", username);. Key should be matching. – Anurag Anand Nov 16 '14 at 04:05
  • @Rasmus you should also check if username or password are not NULL , before setting them as attributes. – Anurag Anand Nov 16 '14 at 06:18
0

Replace user="... by name="... in your HTML and it will work.

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
0

Replace user="... by name="... in your HTML and it will work. NullPointerException is thrown because because of the wrong HTML the request parameters are NULL and these are used for the session lookup.

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58