HttpServletResponse to get Http Status Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buntyindia
    New Member
    • Jun 2007
    • 101

    HttpServletResponse to get Http Status Code

    Hi,

    When we get request on server after processing that it writes back the response to client that also contains HTTP status code like 200 for Ok, 404 for page not found.

    How can I have access to that in java, how can i retrieve that status code. Please help.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The Servlet itself (or any other class in charge) sets that status code itself; there
    is no reason to retrieve it again from the response object, they can remember
    that themselves if they want to. Why do you need that (redundant) functionality?
    Any practical purpose?

    kind regards,

    Jos

    Comment

    • buntyindia
      New Member
      • Jun 2007
      • 101

      #3
      Originally posted by JosAH
      The Servlet itself (or any other class in charge) sets that status code itself; there
      is no reason to retrieve it again from the response object, they can remember
      that themselves if they want to. Why do you need that (redundant) functionality?
      Any practical purpose?

      kind regards,

      Jos
      Before setting that status I want to know the status so that on this basis of the code I can perform certain functionality.

      I am using Filters for logging different kind of logs in this I need to know what is the status of HTTP response.

      Comment

      • itsraghz
        New Member
        • Mar 2007
        • 124

        #4
        How about looking at the
        Code:
        <error-code>
        and
        Code:
        <error-page>
        elements in web.xml?

        Comment

        • weberan
          New Member
          • Dec 2009
          • 1

          #5
          You may obtain the http status line with the following code:

          httpStatusLine = urlConnection.g etHeaderFields( ).get(null).get (0);

          Having this status, you just have to extract the status code :

          static final Pattern HTTP_STATUS_COD E_PATTERN=Patte rn.compile("(\\ d{3})");
          Matcher matcher = HTTP_STATUS_COD E_PATTERN.match er(httpStatusLi ne);
          if (matcher.find() ){
          httpCode = matcher.group(1 );
          }


          Regards,

          André

          Comment

          Working...