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 im 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 try to run the code 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 com.MyServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mainpackage.User;
@WebServlet("/LoginServ")
public class LoginServ 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></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>MyServlet.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>
<servlet>
<description></description>
<display-name>PhonePlansServlet</display-name>
<servlet-name>PhonePlansServlet</servlet-name>
<servlet-class>MyServlet.PhonePlansServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PhonePlansServlet</servlet-name>
<url-pattern>/PhonePlansServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>NewCustomerServlet</display-name>
<servlet-name>NewCustomerServlet</servlet-name>
<servlet-class>MyServlet.NewCustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewCustomerServlet</servlet-name>
<url-pattern>/NewCustomerServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServ</display-name>
<servlet-name>LoginServ</servlet-name>
<servlet-class>com.MyServlet.LoginServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServ</servlet-name>
<url-pattern>/LoginServ</url-pattern>
</servlet-mapping>
</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