0

I'm trying to implement a Tomcat local server, but whenever the method init() of a servlet gets called it all crashes, giving me this page. enter image description here

Here's the method that causes the crash:

'''

 public void init() throws ServletException {
        connection = ConnectionHandler.getConnection(getServletContext());
        ServletContext servletContext = getServletContext();
        ServletContextTemplateResolver templateResolver = new 
              ServletContextTemplateResolver(servletContext);
        templateResolver.setTemplateMode(TemplateMode.HTML);
        this.templateEngine = new TemplateEngine();
        this.templateEngine.setTemplateResolver(templateResolver);
        templateResolver.setSuffix(".html");
    }

'''

The line that crashes is the ServletContextTemplateResolver constructor . The index.html class that calls that method:

'''

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="ISO-8859-1">
  <title>Login page</title>
</head>
<body>
<h1>Welcome!</h1>

<form action="CheckLogin" method="POST">
  Username: <input type="text" name="username" required> <br>
  Password: <input type="password" name="password" required><br>
  <input type="submit" value="login">
  <p th:text=" ${errorMsg}"></p>
</form>
<form action="SignUp" method="POST">
  Username: <input type="text" name="newusername" required> <br>
  Password: <input type="password" name="newpassword" required><br>
  Name: <input type="text" name="name" required> <br>
  Surname: <input type="text" name="surname" required><br>
  <input type="submit" value="sign up">
</form>
</body>
</html>

'''

Here's the pom:

'''

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>it.polimi.tiw</groupId>
    <artifactId>ProgettoTIW</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>ProgettoTIW</name>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.7.1</junit.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api -->
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.11.RELEASE</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jakarta.ejb/jakarta.ejb-api -->
        <dependency>
            <groupId>jakarta.ejb</groupId>
            <artifactId>jakarta.ejb-api</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
    </build>
</project>

'''

From what I've understood I miss a thymeleaf dependency that requires slf4j, but I can't find it. I hope I've inserted everything that's needed to figure out what's wrong.

SergeLover
  • 83
  • 4
  • 1
    See https://stackoverflow.com/a/57118379/104891. You need to add some logging implementation, you only have slf4j api dependency which doesn't work without any implementation. – CrazyCoder Jul 19 '21 at 21:33
  • But how do I do that? I tried to copy and paste the dependency I found on the linked answer, but it doesn't change anything (except for the fact that now I have 3 errors on the pom, one for each line of the new dependency). – SergeLover Jul 20 '21 at 07:51
  • 1
    Make sure to reimport the Maven project: https://stackoverflow.com/a/43192764/104891. – CrazyCoder Jul 20 '21 at 14:25

0 Answers0