How do I throw a 404 error from within a java servlet?
The Servlet API gives you a method to send a 404 or any other HTTP status code. It's the sendError method of HttpServletResponse:
public void doGet(HttpServletRequest request, HttpServletResponse response) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
In your doGet
or doPost
method you have a parameter HttpServletResponse res
404 is a status code which can be set by:
res.setStatus(HttpServletResponse.SC_NOT_FOUND);