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?
+2
A:
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);
}
Ladlestein
2010-07-15 19:04:14
A:
Using the method sendError of HttpServletResponse, you can send errors with an error code. The class also defines constants to represent error codes. For 404, HttpServletResponse.SC_NOT_FOUND.
Marimuthu Madasamy
2010-07-15 19:04:25
A:
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);
stacker
2010-07-15 19:06:47