应用HTTP服务器出现Error 404: NoTargetForURIException问题求助
Let’s walk through the most likely causes for this 404 error and how to fix them, tailored to your setup with CheckServlet and WebSphere:
1. Mismatched or Incorrect Servlet Mapping in web.xml
This is the most common culprit—WebSphere can’t find a Servlet mapped to the URI you’re requesting.
- Check name consistency: Ensure the
<servlet-name>in your<servlet>declaration exactly matches the one in<servlet-mapping>. Even a tiny typo (like lowercase vs uppercase) will break the mapping. - Verify the
url-pattern: Make sure the pattern aligns with the URI you’re using to access the Servlet. For example, if you set<url-pattern>/check-status</url-pattern>, you need to hithttp://your-server:port/your-app-context/check-status(replaceyour-app-contextwith your application’s context root). - Example of a correct mapping:
<servlet> <servlet-name>CheckServlet</servlet-name> <servlet-class>com.your.package.CheckServlet</servlet-class> <!-- Double-check the full package path --> </servlet> <servlet-mapping> <servlet-name>CheckServlet</servlet-name> <url-pattern>/check</url-pattern> </servlet-mapping>
2. Missing or Misplaced Servlet Class in Deployment
WebSphere can’t load your CheckServlet if the compiled .class file isn’t in the right location:
- Check your WAR file structure: The
CheckServlet.classshould live underWEB-INF/classes/com/your/package/(matching your package structure). If you’re using a JAR for the class, ensure it’s placed inWEB-INF/lib. - Re-package your WAR to include the class if it’s missing, then re-deploy the application.
3. WebSphere Deployment or Cache Issues
Sometimes WebSphere fails to load the Servlet correctly even if your config is valid:
- Check the WebSphere Admin Console logs (under Applications > Your App > Logs) for deployment errors. A "class not found" error here means the class isn’t being picked up by the server.
- Clear WebSphere’s temporary files: Delete the contents of
[WAS_INSTALL_DIR]/profiles/[YOUR_PROFILE]/tempand[WAS_INSTALL_DIR]/profiles/[YOUR_PROFILE]/work, then restart the application server. This fixes stale cache issues that might prevent the Servlet from registering.
4. Incorrect Request URI
Double-check the URL you’re using to access the Servlet:
- Don’t forget the application’s context root. If your app is deployed with context root
my-app, the full URL should behttp://server:port/my-app/check(matching yoururl-pattern). - Case matters in most WebSphere setups—for example,
/Checkwon’t match a pattern set to/check.
5. Redundant Servlet Interface Implementation (Minor Cleanup)
While this won’t directly cause a 404, your CheckServlet code has a redundant implements Servlet—since HttpServlet already implements the Servlet interface. You can safely remove that to clean up your code:
public class CheckServlet extends HttpServlet { // Remove "implements Servlet" @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try (PrintWriter out = response.getWriter()) { response.setContentType("text/html"); out.print("<html><body>"); out.printf("Application '%s' is active", getServletContext().getContextName()); out.print("</body></html>"); } } }
内容的提问来源于stack exchange,提问作者Harsha

