I am making a simple Maven Project using Eclipse. There is a form that has Username and Password and a submit button. When I enter and submit the default username(admin) and password(admin). It should either show a page with "Pass" or "Fail" text if they match. But when I submit the form it shows [this error.] (https://i.stack.imgur.com/dw6Rt.png). I am using Tomcat version 10.1 with javax servlet version 4.0.1 for this project and Eclipse EE edition.
This is my file directory for the project.
I have installed the required dependency in the pom.xml file and got the servlet in the web.xml file as well. But I don't know why this issue is still there. I have tried other similar stack overflow solutions and from Google too but none of them worked so far.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="loginServlet" method="POST">
<h1>JSP Page</h1>
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login" id="login">
</form>
</body>
</html>
Success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Test Page</title>
</head>
<body>
<% out.println("Pass"); %>
</body>
</html>
Error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Test Page</title>
</head>
<body>
<% out.println("Fail"); %>
</body>
</html>
LoginServlet.java
package com.examSolution;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("admin") && password.equals("admin")) {
response.sendRedirect("Success.jsp");
}else {
response.sendRedirect("Error.jsp");
}
}
}
It seems like you're encountering an HTTP 405 error, which typically means that the HTTP method used in the request is not supported by the target resource. In your case, it's likely because the servlet is not properly mapped in the web.xml file.
You're using servlet 3.0 annotation-based configuration, so you don't necessarily need entries in the web.xml file. However, if you're using an older version of Tomcat or prefer to configure servlets via web.xml, here's how you can configure it:
In this configuration:
Make sure that your web.xml file is correctly located in the WEB-INF directory of your web application.
If you're using servlet 3.0 or later, you can remove the servlet and servlet-mapping entries from the web.xml file altogether and rely solely on annotations like @WebServlet("/loginServlet") in your servlet class.
Also, ensure that your servlet class is properly compiled and included in the WAR file generated by Maven during the build process.