Eclipse Web项目请求URL



我正在尝试在eclipse中创建一个新的动态web项目。我添加了一个servlet和一些基本的html代码来启动和运行它。但是我有一个问题。当我通过jquery使用url "/news"获取请求时,我得到一个404未发现错误。

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

当我检查完整的请求URL时,我看到http://localhost:8080/news。这是有意义的,因为项目名称也应该在url中。所以当我传递"project-name/news"到get请求时,我仍然得到一个404 not found错误。

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

这一次完整的请求url显示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开头。

在您的情况下,您在请求URL中使用project-name/news,那么结果URL将是http://localhost:8080/project-name/project-name/news,其中project-name是您在示例中的上下文路径。

最新更新