我正在使用的 tomcat 服务器 10.0 服务器一直显示错误 404,因为请求的资源不可用
The tomcat server 10.0 server I am using with my keeps showing me an error 404 for the requested resources are not available
当我尝试 运行 由 eclipse 2021 中的 servlet 控制并使用 tomcat 服务器 10.0 的简单 JSP 登录表单时,错误 404 不断在 eclipse 的内置浏览器 window 上弹出资源不可用,但据我所知,我也在根目录的项目结构中找到了该文件。这是我的代码,有人可以帮我吗?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form action="<%= request.getContextPath()%>/SiteController" method="post">
Username: <input type="text" name="username"> <br/>
Password: <input type="text" name="password"> <br/>
<input type="submit" value="Submit">
</form>
</body>
这是我的login.jsp文件
package org.saboor.servlets;
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;
/**
* Servlet implementation class SiteController
*/
@WebServlet("/SiteController")
public class SiteController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SiteController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username.equals("saboor")&& password.equals("baba2011")) {
request.getSession().invalidate();
HttpSession newSession=request.getSession();
newSession.setMaxInactiveInterval(300);
response.sendRedirect("memberArea.jsp");
}
else {
response.sendRedirect("login.jsp");
}
}
}
这是我的 SiteController.java servlet
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>memberArea</title>
</head>
<body>
memberArea
</body>
</html>
这是登录成功后转到的页面。
My Project Structure
您的 Web 应用程序依赖于 javax.servlet 包,Java EE 规范的一部分,但 Tomcat 10 实现规范开发为雅加达 EE 的一部分。您应该使用 Tomcat 9 而不是实现作为 Java EE 的一部分开发的规范。
来自Migration Guide - Tomcat 10.0.x
There is a significant breaking change between Tomcat 9.0.x and Tomcat 10.0.x. The Java package used by the specification APIs has changed from javax.** to jakarta.** It will be necessary to recompile web applications against the new APIs.
如果必须使用 Tomcat 10,则必须更新所有包引用并切换所有依赖项 (例如,servlet-api,jsp-api、jstl 等) 到符合新 Jakarta EE 规范的库版本。
另一种选择是使用适用于 Jakarta EE 的 Apache Tomcat 迁移工具。
Tomcat can convert an existing web application from Java EE 8 to Jakarta EE 9 at deployment time using the Apache Tomcat migration tool for Jakarta EE.
To make use of the feature, the web application should be placed in the legacyAppBase
folder (by default named webapps-javaee
) and they will be converted to an equivalent Jakarta EE web application in the appBase
folder (by default named webapps
).
但是,迁移会带来一系列挑战,因此,您的情况可能会有所不同。
当我尝试 运行 由 eclipse 2021 中的 servlet 控制并使用 tomcat 服务器 10.0 的简单 JSP 登录表单时,错误 404 不断在 eclipse 的内置浏览器 window 上弹出资源不可用,但据我所知,我也在根目录的项目结构中找到了该文件。这是我的代码,有人可以帮我吗?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form action="<%= request.getContextPath()%>/SiteController" method="post">
Username: <input type="text" name="username"> <br/>
Password: <input type="text" name="password"> <br/>
<input type="submit" value="Submit">
</form>
</body>
这是我的login.jsp文件
package org.saboor.servlets;
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;
/**
* Servlet implementation class SiteController
*/
@WebServlet("/SiteController")
public class SiteController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SiteController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username.equals("saboor")&& password.equals("baba2011")) {
request.getSession().invalidate();
HttpSession newSession=request.getSession();
newSession.setMaxInactiveInterval(300);
response.sendRedirect("memberArea.jsp");
}
else {
response.sendRedirect("login.jsp");
}
}
}
这是我的 SiteController.java servlet
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>memberArea</title>
</head>
<body>
memberArea
</body>
</html>
这是登录成功后转到的页面。
My Project Structure
您的 Web 应用程序依赖于 javax.servlet 包,Java EE 规范的一部分,但 Tomcat 10 实现规范开发为雅加达 EE 的一部分。您应该使用 Tomcat 9 而不是实现作为 Java EE 的一部分开发的规范。
来自Migration Guide - Tomcat 10.0.x
There is a significant breaking change between Tomcat 9.0.x and Tomcat 10.0.x. The Java package used by the specification APIs has changed from javax.** to jakarta.** It will be necessary to recompile web applications against the new APIs.
如果必须使用 Tomcat 10,则必须更新所有包引用并切换所有依赖项 (例如,servlet-api,jsp-api、jstl 等) 到符合新 Jakarta EE 规范的库版本。
另一种选择是使用适用于 Jakarta EE 的 Apache Tomcat 迁移工具。
Tomcat can convert an existing web application from Java EE 8 to Jakarta EE 9 at deployment time using the Apache Tomcat migration tool for Jakarta EE.
To make use of the feature, the web application should be placed in the
legacyAppBase
folder (by default namedwebapps-javaee
) and they will be converted to an equivalent Jakarta EE web application in theappBase
folder (by default namedwebapps
).
但是,迁移会带来一系列挑战,因此,您的情况可能会有所不同。