gpt4 book ai didi

java - HTTP 状态 404 - 未找到 Servlet

转载 作者:行者123 更新时间:2023-11-30 01:54:11 25 4
gpt4 key购买 nike

我在 Eclipse 中有一个项目,并尝试从 Web 浏览器调用 servlet。该图显示了我的项目的结构。虽然我设置了url在注释中我仍然找不到资源。

这是我的代码:

package java.enablingKeyWordSearch;

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;

@WebServlet(asyncSupported = false, name = "HelloServlet", urlPatterns = {"/hello"})
public class TestServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write("<h2>Hello Servlet One </h2>");
out.close();
}
}

project hierarchy

我尝试使用以下命令调用 servlet: http://localhost:8080/MultiKeywordSearch/hello http://localhost:8080/MultiKeywordSearch/src/java/enablingKeyWordSearch/hello

...等等。然而,我收到 HTTP 状态 404 错误。

这是我的web.xml的内容文件:

<?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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MultiKeywordSearch</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

如果有影响的话,我正在使用 Apache Tomcat v8.0。

更新:在新的屏幕截图中显示源列表;删除了 java 命名空间 updated src listing

编辑2.0web.xml:

<?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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MultiKeywordSearch</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>enablingKeyWordSearch.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/multiKeywordSearch</url-pattern>
</servlet-mapping>
</web-app>

enablingKeyWordSearch.TestServlet.java文件:

package enablingKeyWordSearch;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebServlet(asyncSupported = false, name = "HelloServlet", urlPatterns = {"/hello"})
@WebServlet(name = "HelloServlet", urlPatterns = {"/multiKeywordSearch"})
@MultipartConfig
public class TestServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write("<h2>Hello Servlet One </h2>");
out.close();
}
}

遗憾的是仍然收到 404 错误。

最佳答案

@WebServlet("/multiKeywordSearch")

@MultipartConfig


and then try this.
http://localhost:8080/MultiKeyewordSearch/multiKeywordSearch

关于java - HTTP 状态 404 - 未找到 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071882/

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