404 Error Troubleshooting
Common causes for 404 errors include incorrect context path configuration or mismatched servlet mappings. The correct URL format must follow http://host:port/context-root/servlet-path.
Deployment issues may stem from malformed web.xml files, improper directory structures, or incorrect server deployment. Ensure the project adheres to standard JavaEE conventions with WEB-INF containing configuration files and compiled classes.
405 Method Not Allowed Errors
A 405 error occurs when the requested HTTP method isn't supported by the servlet. For example, if a GET request is sent but doGet() isn't overridden, or if super.doGet() remains active without modification. The default implementation of super.doGet() returns a 405 status.
@WebServlet("/invalid")
public class InvalidServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) {
super.doGet(req, res);
}
}
500 Internal Server Errors
500 errors indicate server-side exceptions from unhandled code errors. For instance:
@WebServlet("/error")
public class ErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String data = null;
System.out.println(data.length());
}
}
This triggers a NullPointerException when accessing the length of a null reference.
Blank Page Issues
Blank pages occur when no response content is written to the client. Server-side System.out.println() outputs only to logs, not the HTTP response. Use response.getWriter().write() for client output.
@WebServlet("/blank")
public class BlankServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) {
System.out.println("Server log message");
}
}
Connection Failures
"Site can't be reached" errors appear when the application server (e.g., Tomcat) isn't running or the host/IP/port is misconfigured. Verify server status and network settings.
@WebServlet Annotation Requirements
The @WebServlet annotation requires URL patterns to start with a forward slash (/). Duplicate path mappings in the same application cause deployment failures.
@WebServlet("/correct-path") // Valid
@WebServlet("invalid-path") // Invalid — missing leading slash