我正在使用 Eclipse 制作一个简单的 Maven 项目。有一个包含用户名和密码以及提交按钮的表单。 当我输入并提交默认用户名(admin)和密码(admin)时。如果匹配,它应该显示带有“通过”或“失败”文本的页面。但是当我提交表单时,它显示[此错误。]()。我使用的是 Tomcat 10.1 版和 Eclipse EE 版。
我已经在 pom.xml 文件中安装了所需的依赖项,并在 web.xml 文件中获取了 servlet。但我不知道为什么这个问题仍然存在。我也尝试过其他类似的堆栈溢出解决方案以及来自 Google 的解决方案,但到目前为止它们都不起作用。
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>
成功.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>
错误.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");
}
}
}
您似乎遇到了 HTTP 405 错误,这通常意味着目标资源不支持请求中使用的 HTTP 方法。在您的情况下,可能是因为 servlet 未在 web.xml 文件中正确映射。
您使用的是 servlet 3.0 基于注释的配置,因此不一定需要 web.xml 文件中的条目。但是,如果您使用旧版本的 Tomcat 或者更喜欢通过 web.xml 配置 servlet,则可以按以下方式配置它:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>SimpleMavenProject</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.examSolution.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
在此配置中:
确保您的 web.xml 文件正确位于 Web 应用程序的 WEB-INF 目录中。
如果您使用 servlet 3.0 或更高版本,则可以从 web.xml 文件中完全删除 servlet 和 servlet 映射条目,并仅依赖 servlet 类中的 @WebServlet("/loginServlet") 等注释。
此外,请确保您的 servlet 类已正确编译并包含在构建过程中 Maven 生成的 WAR 文件中。