How do I throw a 404 error from within a java servlet?

Viewed 70082

How do I throw a 404 error from within a java servlet? My web.xml already specifies what page to show when there is a 404, how do I throw a 404 from within a servlet?

3 Answers

For adding Request URL with 404 use this below code

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
}
Related