“404:源服务器没有找到目标资源的当前表示或者不愿意公开该表示存在”与雄猫一起
当我尝试在Tomcat服务器上运行我的Web应用程序项目时,我会收到以下错误。
说明原始服务器没有找到目标资源的当前表示形式,也不愿意透露一个存在。
我真的不知道下面的代码有什么问题。我在Stackoverflow上阅读了类似的问题,但是找不到可以在项目中实现的任何答案。感谢任何帮助。
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>$Title$</title>
</head>
<body>
<h1>Hello</h1>
<ul>
<li><a href="/register">Register</a></li>
<li><a href="/login">Login</a></li>
<li><a href="/panel">Panel</a> </li>
<li><a href="/logout">Logout</a> </li>
</ul>
<c:forEach items="${posts}" var="post">
<p>
<h4><c:out value="${post.title} ${post.author}"/><br /></h4>
<c:out value="${post.text}"/>
<a href="/post/${post.id}">Read more</a>
</p>
</c:forEach>
</body>
</html>
homepage.java
import Database.DBAdminConnector; import Database.DBUserConnector;
import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.sql.*; import java.util.ArrayList; import java.util.List;
@WebServlet(name = "HomePage", urlPatterns = "/") public class HomePage extends HttpServlet {
Statement statement = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
DBUserConnector dbConnector = DBUserConnector.INSTANCE;
Connection connection = dbConnector.getConnection();
resp.setContentType("text/html");
try {
statement = connection.createStatement();
String getPosts = "SELECT * FROM latest LIMIT 10";
ResultSet posts = statement.executeQuery(getPosts);
List<Post> postList = new ArrayList<>();
while(posts.next()) {
int id = posts.getInt("id");
String title = posts.getString("title");
String author = posts.getString("nickname");
Date date = posts.getDate("time_created");
String text = posts.getString("text");
Post p = new Post(id, title, author, date, text);
//System.out.println(p);
postList.add(p);
}
req.setAttribute("posts", postList);
RequestDispatcher view = req.getRequestDispatcher("index.jsp");
view.forward(req,resp);
} catch (SQLException e) {
e.printStackTrace();
}
} }
web.xml
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false">
<servlet-mapping>
<servlet-name>HomePage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PostPage</servlet-name>
<url-pattern>/post/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterPage</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginPage</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PanelPage</servlet-name>
<url-pattern>/panel</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>BackupRestoreDB</servlet-name>
<url-pattern>/backup</url-pattern>
</servlet-mapping>
</web-app>
When I try to run my web app project on Tomcat server I'm getting the following error.
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I have really no idea what is wrong with my code below. I've read similar questions on StackOverflow but I can't find any answer that I could implement into my project. I appreciate any help.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>$Titlelt;/title>
</head>
<body>
<h1>Hello</h1>
<ul>
<li><a href="/register">Register</a></li>
<li><a href="/login">Login</a></li>
<li><a href="/panel">Panel</a> </li>
<li><a href="/logout">Logout</a> </li>
</ul>
<c:forEach items="${posts}" var="post">
<p>
<h4><c:out value="${post.title} ${post.author}"/><br /></h4>
<c:out value="${post.text}"/>
<a href="/post/${post.id}">Read more</a>
</p>
</c:forEach>
</body>
</html>
HomePage.java
import Database.DBAdminConnector; import Database.DBUserConnector;
import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.sql.*; import java.util.ArrayList; import java.util.List;
@WebServlet(name = "HomePage", urlPatterns = "/") public class HomePage extends HttpServlet {
Statement statement = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
DBUserConnector dbConnector = DBUserConnector.INSTANCE;
Connection connection = dbConnector.getConnection();
resp.setContentType("text/html");
try {
statement = connection.createStatement();
String getPosts = "SELECT * FROM latest LIMIT 10";
ResultSet posts = statement.executeQuery(getPosts);
List<Post> postList = new ArrayList<>();
while(posts.next()) {
int id = posts.getInt("id");
String title = posts.getString("title");
String author = posts.getString("nickname");
Date date = posts.getDate("time_created");
String text = posts.getString("text");
Post p = new Post(id, title, author, date, text);
//System.out.println(p);
postList.add(p);
}
req.setAttribute("posts", postList);
RequestDispatcher view = req.getRequestDispatcher("index.jsp");
view.forward(req,resp);
} catch (SQLException e) {
e.printStackTrace();
}
} }
web.xml
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false">
<servlet-mapping>
<servlet-name>HomePage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PostPage</servlet-name>
<url-pattern>/post/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterPage</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginPage</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PanelPage</servlet-name>
<url-pattern>/panel</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>BackupRestoreDB</servlet-name>
<url-pattern>/backup</url-pattern>
</servlet-mapping>
</web-app>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是整个频谱的应用程序服务器的常见问题。如果您遇到404或其他问题,调试问题的方法是逐步向前逐步向前。您应该做的第一件事是查看服务器日志,以确保服务器已正确启动。如果有的话,您应该尝试创建一个index.html文件并对其进行测试。等等。在一次失败测试中,您退后一步,查看前一步中出了什么问题。在成功的测试中,您向前迈进,看看下一件事是否有效
This is a common problem with application servers across the full spectrum. If you are encountering 404s or other issues, the way to debug the issue is to incrementally step backwards and forwards. The first thing you should do is look at your server logs to make sure the server has started right. If it has, you should try creating a index.html file and testing that. And so on and so on. On a failed test, you step back to see what's wrong in the previous steps. On a successful test, you step forward to see if the next thing works
我遇到了同样的问题,我使用的是 Eclipse 版本 4.25.0,它使用 JDK 17。
我使用的是 Tomcat 8。服务器启动正常(如 Eclipse 中所示),但是当点击 url 时,它给出了这个错误。然后我检查了服务器日志。
检查了以下服务器日志 - 在哪里可以查看 Tomcat 日志文件在 Eclipse 中?
在服务器日志中发现以下错误 -
请参阅 - 如何在 Maven pom.xml 中正确配置 Jakarta EE 库对于 Tomcat
Tomcat 10.x 是第一个 Jakartified 版本,而不是 Tomcat 9.x。
因此,我安装了 Tomcat 10.x,使用该服务器运行我的应用程序,服务器日志中没有出现错误 - 并且应用程序运行良好!
祝你好运。
I faced the same issue, I was on eclipse version 4.25.0 which was using JDK 17.
I was using Tomcat 8. The server was starting fine (as shown in Eclipse) but when url was hit, it gave this error. Then I checked the server log.
Checked the server log following this- Where can I view Tomcat log files in Eclipse?
Found the following error in server log -
See - How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat
Tomcat 10.x was the first version to be Jakartified, not Tomcat 9.x.
So I installed Tomcat 10.x, used that server to run my application, got no error in the server log-- and application ran fine!
Good luck.
在Eclipse上创建一个新的动态Web项目并尝试运行它后,我遇到了相同的错误消息。我在类似的stackoverflow问题上找不到解决方案。我两年前使用旧版本的Eclipse创建的项目被导入我当今使用的最新版本的Eclipse,他们没有提供错误消息。这暗示了问题与项目本身而不是IDE有关。将一个较旧的项目与新项目进行比较之后,我更改了构建路径的构建类路径顺序(在“订单和导出”选项卡上)将SRC/MAIN/JAVA移至第一名。它起作用。
After creating a new dynamic web project on Eclipse and try to run it, I encountered the same error message. I couldn't find the solution on similar questions of StackOverflow. Projects I have created two years ago using an older version of Eclipse were imported to the newest version of Eclipse I'm using nowadays and they did not present the error message. That gave the hint that the problem was related to the project itself rather than the IDE. After comparing an older project with the new one, I changed the build class path order of the build path (on "Order and Export" tab) moving the src/main/java up to the first place. It worked.