0

So I'm trying to create a WebApp on InteliJ Ultimate about a telecom website and I wanna get familiar with servlets but i can't get them to work properly

<!DOCTYPE html>
<html>
<head>
    <title>Login Page</title>
</head>
<body>
    <h2>Login Form</h2>
    <form action="LoginServ" method="post">
        <label for="username">Username:</label><br>
        <input type="text" id="username" name="username"><br>
        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

this is my index.html (basic login page) that im trying to redirect to the login servlet to then show 1 of 3 different dashboards (clientDashboard, salesmanDashboard and adminDashboard) depending on a value inserted in a MySQL database

I've tried following different tutorials on how to use servlets couldn't get anything to work and now I'm just at a point where I just don't know what to do anymore

Edit 1: I keep getting Error 404 Not found when I redirect from the html page to the servlet which seems odd to me seeing as how I have the servlet file LoginServ.java in the directory along with this line of code in the index file <form action="LoginServ" method="post"> which I am pretty sure searches for a file with that name (don't quote me on that I'm still new to web dev)

At this points im just trying to get the redirect to work at all

package servlets;

import jakarta.servlet.RequestDispatcher;
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("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect(request.getContextPath() + "/salesmanDashboard.jsp");
    }
}

pom.xml:

<?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>com.example</groupId>
    <artifactId>Ergasia2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Ergasia 2</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <junit.version>5.10.0</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-api</artifactId>
            <version>9.0.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-cdi2-se</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>5.1.2.Final</version>
        </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.4.0</version>
            </plugin>
        </plugins>
    </build>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0">
  <servlet>
    <description/>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>servlets.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <display-name>Ergasia 2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

I'm using Apache Tomcat 10.1.24 When i was creating the project i selected "Jakarta EE" as the Project Generator with Maven as the build sustem, also im using Oracle OpenJDK 22.0.1

My directory looks like this:

Project1
-src
  -main 
    -java
      -servlets
        - LoginServlet
  -resources
    - META-INF
      - index.html
  -webapp
    - WEB-INF
      - salesmanDashboard.jsp

Edit 2: The problem looks to be that the servlet mapping i had written in the web.xml file interfered with the @WebServlet("/LoginServlet") that was in the Servlet i was trying to run. I don't understand why but it did and removing the

<servlet>
    <description/>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>servlets.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>

From the web.xml file seemed to fix it

7
  • 1
    Are you sure you want to redirect? Or just forward to the JSP page? You may want to include the directory structure of the files of your web app in your question.
    – prasad_
    Commented Jun 21 at 7:14
  • See also How to avoid compatibility issues between Java EE and Jakarta EE?. You are using Tomcat 10 which requires Jakarta EE but yourr applications uses Java EE.
    – dan1st
    Commented Jun 21 at 7:40
  • Yes, your pom looks convincing but not your imports. Should be import jakarta.servlet.http.HttpServlet; etc.
    – g00se
    Commented Jun 21 at 8:16
  • @dan1st It is not duplicate because the answers assume JSP pages can be served directly and didn't explain how to access them under WEB-INF folder.
    – Roman C
    Commented Jun 21 at 9:49
  • index.html <form action="LoginServ" method="post"> , but /LoginServlet, will always get 404
    – life888888
    Commented Jun 21 at 10:20

1 Answer 1

0

You can't redirect to JSP if it is under WEB-INF folder. Instead, you can use a request dispatcher to retrieve it from there. For this purpose you should implement doGet() method:

protected void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext()
                .getRequestDispatcher("/WEB-INF/salesmanDashboard.jsp");
        dispatcher.forward(request, response);        
    }

protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect(request.getContextPath() + "/LoginServlet");
    }
3
  • 1
    thanks a lot for the help lads i managed to fix it oddly by just removing the servlet mapping from the web.xml I don't really understand why it was interfering with the @WebServlet and giving me an "error 404 not found" but alas it was and removing the servlet mapping fixed it. thanks again for the code snippet @Roman C , now i just gotta go figure out how i wanna go about having 3 different doGet's and doPost's for the objects in the database Commented Jun 21 at 10:14
  • index.html <form action="LoginServ" method="post"> , but /LoginServlet, will always get 404
    – life888888
    Commented Jun 21 at 10:20
  • @life888888 yes i realized it a little after posting the question but even after changing it to <form action="LoginServlet" method="post"> i was still getting the 404 not found error Commented Jun 21 at 10:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.