Search code examples
jsptomcatservlets

Servlet not invoked


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.


Solution

  • 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.