我在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();
}
}

我尝试使用以下方法调用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>我正在使用ApacheTomcatv8.0,如果这有区别的话。
更新:在新屏幕截图中显示源代码清单;删除java命名空间

编辑2.0 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>
<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错误。
发布于 2019-03-09 07:11:04
@WebServlet("/multiKeywordSearch")
@MultipartConfig
and then try this.
http://localhost:8080/MultiKeyewordSearch/multiKeywordSearch发布于 2019-03-09 07:25:17
尝试更新您的web.xml
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>java.enablingKeyWordSearch.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>https://stackoverflow.com/questions/55071882
复制相似问题