Reputation: 469
I have a webapp deployed in Tomcat 8. This is the servlet I am having problems with:
@WebServlet(name = "Start", urlPatterns = {"/start"})
public class StartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestBodyStr = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
StartRequest startRequest = JsonUtil.parseStartRequest(requestBodyStr);
if (startRequest == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
response.setStatus(HttpServletResponse.SC_OK);
String responseBody = getResponseBody();
try (PrintWriter out = response.getWriter()) {
out.print(responseBody);
}
}
}
If I make a request to this servlet then the response contains no OK status message, it is just:
HTTP/1.1 200
How do I add OK to the response?
Upvotes: 1
Views: 1220
Reputation: 1656
When you do response.setStatus(HttpServletResponse.SC_OK);
, it is the same as doing response.setStatus(200);
(Literally, if you check it, SC_OK
is just a constant with value 200
). 200 is the code for OK
in http. If you want to write OK
manually, you have to add:
responserBody += " OK";
try (PrintWriter out = response.getWriter()) {
out.print(responseBody);
}
but that shouldn't be useful, as 200
is already saying: The request worked as intended.
Upvotes: 1