0

I am trying to make a simple Login form. User must enter the email and password, click the login button and after that he must be redirected to the home page. The Login jsp website loads, but the Servlet for that website doesn't work. I don't know what's the reason for that. I am using TomCat as the server.

Here is my JSP file - login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Rubik:400,700'>
    <link rel="stylesheet" href="styleLogin.css">

</head>
<body>
<div class="login-form">
    <form>
        <h1>Login</h1>
        <div class="content">
            <div class="input-field">
                <label>
                    <input type="email" placeholder="Email" autocomplete="nope">
                </label>
            </div>
            <div class="input-field">
                <label>
                    <input type="password" placeholder="Password" autocomplete="new-password">
                </label>
            </div>
        </div>
        <div class="action">
            <button>Sign in</button>
        </div>
    </form>
</div>
</body>
</html>

Here is the LoginServlet (it must work on that login.jsp)

package com.example.userbook.servlet;

import com.example.userbook.manager.UserManager;
import com.example.userbook.model.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    UserManager userManager = new UserManager();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("DoPost triggered"); // Testing if the servlet even starts
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        User user = userManager.getUserByEmailAndPassword(email, password);

        if (user == null) {
            req.getRequestDispatcher("/login").forward(req, resp);


        } else {
            req.getRequestDispatcher("/").forward(req, resp);
        }
    }
}


Here is the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.example.userbook.servlet.LoginServlet</servlet-class>
        <jsp-file>/login.jsp</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

</web-app>

Here is the index.jsp (first loading page, there is just a button there to go to the login.jsp)

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello, please proceed to the login page" %>
</h1>
<br/>
<button onclick="window.location.href='/login'">Login</button>
</body>
</html>

I am trying to get that, once the server starts, it loads that index.jsp and I click the button that goes to the login website and from there user inputs his email, password. I have a MySQL database connected to the project, so in LoginServlet you may see some parts of it. It basically checks if the user exists or not. If not he is redirected to the login website again, to try again otherwise he is redirected to the beginning (index.jsp).

The problem is that whenever I click on the "login" button on the index.jsp website, I am redirected to the login.jsp but the LoginServlet is not even triggered.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It's not a problem. Isn't it an expected workflow? If you pressed a login button then it should go to the login page. – Roman C Aug 20 '23 at 08:33
  • Yes it is, but it's only in the code how it looks. When running the program the Servlet is not invoked. You can see the print statement in the beginning to test it. It is not printed. I have almost the same "register" page as well. And the same issue persists there. – Jack Cumber Aug 20 '23 at 09:18
  • You change the URL to `/login`, but shouldn't that be prefixed with the application's context path? – Rob Spoor Aug 20 '23 at 12:18
  • Sorry, change which URL? Also I associated everything (as far as I understand) in **web.xml** file – Jack Cumber Aug 20 '23 at 12:26
  • How did you try to invoke a servlet? – Roman C Aug 20 '23 at 12:39
  • Isn't the Servlet invoked when loading the "/login" page? Based on the **web.xml** and the **@WebServlet** annotation in LoginServlet – Jack Cumber Aug 20 '23 at 12:46
  • Yes, it's invoked. How did you know that? – Roman C Aug 20 '23 at 14:52
  • Know what? Whenever I am going to the Login website, I enter the email and password, hit the "Sign in" button and nothing happens. And the testing print statement doesn't even print. And the redirections don't work as well. Meaning that the website is loaded, but the LoginServlet is not invoked. – Jack Cumber Aug 20 '23 at 14:54
  • The button is doing nothing. To invoke LoginServlet you should post the form. – Roman C Aug 20 '23 at 19:03
  • I tried the answer by **eis**, but it didn't work. Are you talking about the same thing? – Jack Cumber Aug 20 '23 at 20:20

2 Answers2

1

Your login servlet has a doPost method, but you're not doing a POST request with your form. You'll need to change your login.jsp to contain a form with post method and a form submit button. Additionally, you'll need to introduce action parameter in the form to point to your servlet.

Stuff you're missing:

<form method="post" action="/login">
   <input type="submit" value="Login"/>
</form>
eis
  • 51,991
  • 13
  • 150
  • 199
0

The issue was that, I was using Tomcat version 9 instead of Tomcat version 10, which is the latest and it is the one which supports the change of Java EE to Jakarta EE. Basically if you are using Jakarta EE, you must use Tomcat version 10 and higher.