Retrieveing status code from a response in java

1.4k views Asked by At

How to retrieve a response code for an error page? For successful page I am using getresponsecode method. But for an error page, which method I have to use to get a status code of a response of web application?

2

There are 2 answers

2
icza On

HttpServletResponse.getStatus() is the one you're looking for. It is not just for error pages, it is for every servlet responses.

The HttpServletResponse class contains constants for the possible values, e.g.

  • HttpServletResponse.SC_OK for success (200)
  • HttpServletResponse.SC_BAD_REQUEST for indicating a bad request (400)
0
shikjohari On

httpServletResponse.getStatus() will give you an int value which represents the status code. Open HttpServletResponse class an you ll find the list of the possible status codes