gpt4 book ai didi

java - Eclipse Web 项目请求 URL

转载 作者:行者123 更新时间:2023-12-01 12:41:34 31 4
gpt4 key购买 nike

我正在尝试在 Eclipse 中创建一个新的动态 Web 项目。我添加了一个 servlet 和一些基本的 html 代码来启动并运行它。不过我有一个问题。当我使用 url“/news”通过 jquery 发出 get 请求时,出现 404 未找到错误。

$.get( // resulting url http://localhost:8080/news
"/news",
function(data) {
alert('page content: ' + data);
}
, "html");

当我检查完整的请求 URL 时,我看到 http://localhost:8080/news。这是有道理的,因为项目名称也应该在 url 中。因此,当我将“项目名称/新闻”传递到 get 请求时,我仍然收到 404 未找到错误。

$.get( // resulting url http://localhost:8080/project-name/project-name/news
"project-name/news",
function(data) {
alert('page content: ' + data);
}
, "html");

这次完整的请求网址显示http://localhost:8080/project-name/project-name/news...当我手动向http发出请求时://localhost:8080/project-name/news 它工作得很好。那么为什么 Eclipse 将项目名称添加到 url 中呢?

Servlet 声明

<servlet>
<servlet-name>NewsServlet</servlet-name>
<servlet-class>com.crosbygames.servlet.NewsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewsServlet</servlet-name>
<url-pattern>/news</url-pattern>
</servlet-mapping>

最佳答案

当您在请求 URL 中以“/news”开头时,它将以 http://localhost:8080/news 开头.

但是如果你使用“news”(没有斜杠“/”),它将以 http://localhost:8080/<content path>/news 开头.

就您而言,您正在使用 project-name/news在您的请求 URL 中,则生成的 URL 将是 http://localhost:8080/project-name/project-name/news哪里project-name是您示例中的上下文路径。

关于java - Eclipse Web 项目请求 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25050944/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com