You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse项目调用Servlet遇HTTP Status 404错误求助

Fixing HTTP 404 Error for Servlet in Eclipse + Tomcat 8.0

Let's break down why your TestServlet is throwing a 404 error and get it working step by step, based on your code and setup:

1. Fix Package Name Mismatch

You changed the servlet's package from java.enablingKeyWordSearch to enablingKeyWordSearch—this is a common source of 404s if not consistent everywhere:

  • Double-check your servlet's package declaration is correct:
    package enablingKeyWordSearch;
    
  • If using web.xml mapping, ensure the <servlet-class> matches exactly:
    <servlet-class>enablingKeyWordSearch.TestServlet</servlet-class>
    
  • Verify the compiled .class file exists in WEB-INF/classes/enablingKeyWordSearch/ inside your deployed project (check Tomcat's webapps/MultiKeywordSearch folder or Eclipse's build output directory).

2. Avoid Mixing Annotation and web.xml Configurations

Tomcat can get confused if you use both @WebServlet annotation and web.xml mapping for the same servlet. Pick one approach:

Option A: Use Only @WebServlet Annotation

  • Revert your web.xml to its original version (remove the <servlet> and <servlet-mapping> sections).
  • Update the servlet annotation to clearly define your desired URL patterns:
    @WebServlet(name = "HelloServlet", urlPatterns = {"/hello", "/multiKeywordSearch"})
    @MultipartConfig
    public class TestServlet extends HttpServlet {
        // ... your doGet method code ...
    }
    
  • Restart Tomcat and try accessing http://localhost:8080/MultiKeywordSearch/hello or http://localhost:8080/MultiKeywordSearch/multiKeywordSearch.

Option B: Use Only web.xml Mapping

  • Comment out or remove the @WebServlet annotation from your TestServlet class.
  • Keep your modified web.xml but confirm all values align:
    <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>
    
  • Restart Tomcat and access http://localhost:8080/MultiKeywordSearch/multiKeywordSearch.

3. Verify Project Deployment in Tomcat

  • In Eclipse's Servers view, right-click your Tomcat server and select Add and Remove...—make sure MultiKeywordSearch is listed under Configured (not Available).
  • Check the deployment folder: Navigate to Tomcat's webapps directory (usually apache-tomcat-8.0.x/webapps) and confirm there's a MultiKeywordSearch folder with a valid WEB-INF structure.
  • Confirm the context path: Visit http://localhost:8080/ to see Tomcat's welcome page, then use the "Manager App" (enable it if needed) to verify your project's context path is indeed /MultiKeywordSearch.

4. Check Eclipse Build Configuration

  • Ensure your project's Target Runtime is set to Apache Tomcat v8.0: Right-click the project → Properties → Target Runtime → Select Tomcat 8.0.
  • Verify compiled classes output to WEB-INF/classes: Right-click project → Properties → Java Build Path → Output Folder → Ensure it points to a directory that gets copied to WEB-INF/classes during deployment.

Quick Side Note

Not related to the 404, but your current output code uses escaped HTML:

out.write("&lt;h2&gt;Hello Servlet One &lt;/h2&gt;");

To render the heading properly, change it to:

out.write("<h2>Hello Servlet One </h2>");

内容的提问来源于stack exchange,提问作者i_use_the_internet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:30:57