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.xmlmapping, ensure the<servlet-class>matches exactly:<servlet-class>enablingKeyWordSearch.TestServlet</servlet-class> - Verify the compiled
.classfile exists inWEB-INF/classes/enablingKeyWordSearch/inside your deployed project (check Tomcat'swebapps/MultiKeywordSearchfolder 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.xmlto 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/helloorhttp://localhost:8080/MultiKeywordSearch/multiKeywordSearch.
Option B: Use Only web.xml Mapping
- Comment out or remove the
@WebServletannotation from yourTestServletclass. - Keep your modified
web.xmlbut 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
MultiKeywordSearchis listed under Configured (not Available). - Check the deployment folder: Navigate to Tomcat's
webappsdirectory (usuallyapache-tomcat-8.0.x/webapps) and confirm there's aMultiKeywordSearchfolder with a validWEB-INFstructure. - 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 toWEB-INF/classesduring deployment.
Quick Side Note
Not related to the 404, but your current output code uses escaped HTML:
out.write("<h2>Hello Servlet One </h2>");
To render the heading properly, change it to:
out.write("<h2>Hello Servlet One </h2>");
内容的提问来源于stack exchange,提问作者i_use_the_internet
相关产品推荐
相关产品推荐

