Is it possible in Java to return the 401 Unauthorized response code explicitly
For error status codes like 401, use the more specific sendError():
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "your message goes here");
This takes care of everything, it sets the status code and also writes the response.
assuming you are using servlets, you would set the http status to 401 using the setStatus
method:
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
HttpServletResponse info