Response status code java

Содержание
  1. Response status code java
  2. Nested Class Summary
  3. Method Summary
  4. Method Detail
  5. statusCode
  6. request
  7. previousResponse
  8. headers
  9. body
  10. sslSession
  11. uri
  12. version
  13. Response status code java
  14. Nested Class Summary
  15. Enum Constant Summary
  16. Method Summary
  17. Methods inherited from class java.lang.Enum
  18. Methods inherited from class java.lang.Object
  19. Enum Constant Detail
  20. OK
  21. CREATED
  22. ACCEPTED
  23. NO_CONTENT
  24. RESET_CONTENT
  25. PARTIAL_CONTENT
  26. MOVED_PERMANENTLY
  27. FOUND
  28. SEE_OTHER
  29. NOT_MODIFIED
  30. USE_PROXY
  31. TEMPORARY_REDIRECT
  32. BAD_REQUEST
  33. UNAUTHORIZED
  34. PAYMENT_REQUIRED
  35. FORBIDDEN
  36. NOT_FOUND
  37. METHOD_NOT_ALLOWED
  38. NOT_ACCEPTABLE
  39. PROXY_AUTHENTICATION_REQUIRED
  40. REQUEST_TIMEOUT
  41. CONFLICT
  42. GONE
  43. LENGTH_REQUIRED
  44. PRECONDITION_FAILED
  45. REQUEST_ENTITY_TOO_LARGE
  46. REQUEST_URI_TOO_LONG
  47. UNSUPPORTED_MEDIA_TYPE
  48. REQUESTED_RANGE_NOT_SATISFIABLE
  49. EXPECTATION_FAILED
  50. INTERNAL_SERVER_ERROR
  51. NOT_IMPLEMENTED
  52. BAD_GATEWAY
  53. SERVICE_UNAVAILABLE
  54. GATEWAY_TIMEOUT
  55. HTTP_VERSION_NOT_SUPPORTED
  56. Method Detail
  57. values
  58. valueOf
  59. getFamily
  60. getStatusCode
  61. getReasonPhrase
  62. toString
  63. fromStatusCode
  64. Response status code java
  65. Nested Class Summary
  66. Enum Constant Summary
  67. Method Summary
  68. Methods inherited from class java.lang.Enum
  69. Methods inherited from class java.lang.Object
  70. Methods inherited from interface javax.ws.rs.core.Response.StatusType
  71. Enum Constant Detail
  72. OK
  73. CREATED
  74. ACCEPTED
  75. NO_CONTENT
  76. RESET_CONTENT
  77. PARTIAL_CONTENT
  78. MOVED_PERMANENTLY
  79. FOUND
  80. SEE_OTHER
  81. NOT_MODIFIED
  82. USE_PROXY
  83. TEMPORARY_REDIRECT
  84. BAD_REQUEST
  85. UNAUTHORIZED
  86. PAYMENT_REQUIRED
  87. FORBIDDEN
  88. NOT_FOUND
  89. METHOD_NOT_ALLOWED
  90. NOT_ACCEPTABLE
  91. PROXY_AUTHENTICATION_REQUIRED
  92. REQUEST_TIMEOUT
  93. CONFLICT
  94. GONE
  95. LENGTH_REQUIRED
  96. PRECONDITION_FAILED
  97. REQUEST_ENTITY_TOO_LARGE
  98. REQUEST_URI_TOO_LONG
  99. UNSUPPORTED_MEDIA_TYPE
  100. REQUESTED_RANGE_NOT_SATISFIABLE
  101. EXPECTATION_FAILED
  102. PRECONDITION_REQUIRED
  103. TOO_MANY_REQUESTS
  104. REQUEST_HEADER_FIELDS_TOO_LARGE
  105. INTERNAL_SERVER_ERROR
  106. NOT_IMPLEMENTED
  107. BAD_GATEWAY
  108. SERVICE_UNAVAILABLE
  109. GATEWAY_TIMEOUT
  110. HTTP_VERSION_NOT_SUPPORTED
  111. NETWORK_AUTHENTICATION_REQUIRED
  112. Method Detail
  113. values
  114. valueOf
  115. getFamily
  116. getStatusCode
  117. getReasonPhrase
  118. toString
  119. fromStatusCode
  120. Setting the HTTP status code
  121. When to set the status code
  122. Always send output
  123. Status code constants
  124. Common status codes

Response status code java

An HTTP response. An HttpResponse is not created directly, but rather returned as a result of sending an HttpRequest . An HttpResponse is made available when the response status code and headers have been received, and typically after the response body has also been completely received. Whether or not the HttpResponse is made available before the response body has been completely received depends on the BodyHandler provided when sending the HttpRequest . This class provides methods for accessing the response status code, headers, the response body, and the HttpRequest corresponding to this response. The following is an example of retrieving a response as a String:

 HttpResponse response = client .send(request, BodyHandlers.ofString()); 

The class BodyHandlers provides implementations of many common response handlers. Alternatively, a custom BodyHandler implementation can be used.

Читайте также:  Object get keys java

Nested Class Summary

Implementations of BodyHandler that implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.

Implementations of BodySubscriber that implement various useful subscribers, such as converting the response body bytes into a String, or streaming the bytes to a file.

Initial response information supplied to a BodyHandler when a response is initially received and before the body is processed.

Method Summary

Method Detail

statusCode

request

Returns the HttpRequest corresponding to this response. The returned HttpRequest may not be the initiating request provided when sending. For example, if the initiating request was redirected, then the request returned by this method will have the redirected URI, which will be different from the initiating request URI.

previousResponse

OptionalHttpResponseT>> previousResponse()

Returns an Optional containing the previous intermediate response if one was received. An intermediate response is one that is received as a result of redirection or authentication. If no previous response was received then an empty Optional is returned.

headers

body

Returns the body. Depending on the type of T , the returned body may represent the body after it was read (such as byte[] , or String , or Path ) or it may represent an object with which the body is read, such as an InputStream . If this HttpResponse was returned from an invocation of previousResponse() then this method returns null

sslSession

Returns an Optional containing the SSLSession in effect for this response. Returns an empty Optional if this is not a HTTPS response.

uri

Returns the URI that the response was received from. This may be different from the request URI if redirection occurred.

version

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Response status code java

Commonly used status codes defined by HTTP, see HTTP/1.1 documentation for the complete list. Additional status codes can be added by applications by creating an implementation of Response.StatusType .

Nested Class Summary

Enum Constant Summary

Method Summary

Methods inherited from class java.lang.Enum

Methods inherited from class java.lang.Object

Enum Constant Detail

OK

CREATED

ACCEPTED

NO_CONTENT

RESET_CONTENT

PARTIAL_CONTENT

MOVED_PERMANENTLY

FOUND

SEE_OTHER

NOT_MODIFIED

USE_PROXY

TEMPORARY_REDIRECT

BAD_REQUEST

UNAUTHORIZED

PAYMENT_REQUIRED

FORBIDDEN

NOT_FOUND

METHOD_NOT_ALLOWED

NOT_ACCEPTABLE

PROXY_AUTHENTICATION_REQUIRED

public static final Response.Status PROXY_AUTHENTICATION_REQUIRED

REQUEST_TIMEOUT

CONFLICT

GONE

LENGTH_REQUIRED

PRECONDITION_FAILED

REQUEST_ENTITY_TOO_LARGE

REQUEST_URI_TOO_LONG

UNSUPPORTED_MEDIA_TYPE

REQUESTED_RANGE_NOT_SATISFIABLE

public static final Response.Status REQUESTED_RANGE_NOT_SATISFIABLE

EXPECTATION_FAILED

INTERNAL_SERVER_ERROR

NOT_IMPLEMENTED

BAD_GATEWAY

SERVICE_UNAVAILABLE

GATEWAY_TIMEOUT

HTTP_VERSION_NOT_SUPPORTED

public static final Response.Status HTTP_VERSION_NOT_SUPPORTED

Method Detail

values

Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:

for (Response.Status c : Response.Status.values()) System.out.println(c);

valueOf

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

getFamily

getStatusCode

getReasonPhrase

toString

fromStatusCode

public static Response.Status fromStatusCode(int statusCode)

Источник

Response status code java

Commonly used status codes defined by HTTP, see HTTP/1.1 documentation for the complete list. Additional status codes can be added by applications by creating an implementation of Response.StatusType .

Nested Class Summary

Enum Constant Summary

Method Summary

Methods inherited from class java.lang.Enum

Methods inherited from class java.lang.Object

Methods inherited from interface javax.ws.rs.core.Response.StatusType

Enum Constant Detail

OK

CREATED

ACCEPTED

NO_CONTENT

RESET_CONTENT

PARTIAL_CONTENT

MOVED_PERMANENTLY

FOUND

SEE_OTHER

NOT_MODIFIED

USE_PROXY

TEMPORARY_REDIRECT

BAD_REQUEST

UNAUTHORIZED

PAYMENT_REQUIRED

FORBIDDEN

NOT_FOUND

METHOD_NOT_ALLOWED

NOT_ACCEPTABLE

PROXY_AUTHENTICATION_REQUIRED

public static final Response.Status PROXY_AUTHENTICATION_REQUIRED

REQUEST_TIMEOUT

CONFLICT

GONE

LENGTH_REQUIRED

PRECONDITION_FAILED

REQUEST_ENTITY_TOO_LARGE

REQUEST_URI_TOO_LONG

UNSUPPORTED_MEDIA_TYPE

REQUESTED_RANGE_NOT_SATISFIABLE

public static final Response.Status REQUESTED_RANGE_NOT_SATISFIABLE

EXPECTATION_FAILED

PRECONDITION_REQUIRED

TOO_MANY_REQUESTS

REQUEST_HEADER_FIELDS_TOO_LARGE

public static final Response.Status REQUEST_HEADER_FIELDS_TOO_LARGE

INTERNAL_SERVER_ERROR

NOT_IMPLEMENTED

BAD_GATEWAY

SERVICE_UNAVAILABLE

GATEWAY_TIMEOUT

HTTP_VERSION_NOT_SUPPORTED

public static final Response.Status HTTP_VERSION_NOT_SUPPORTED

NETWORK_AUTHENTICATION_REQUIRED

public static final Response.Status NETWORK_AUTHENTICATION_REQUIRED

Method Detail

values

Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:

for (Response.Status c : Response.Status.values()) System.out.println(c);

valueOf

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

getFamily

getStatusCode

getReasonPhrase

toString

fromStatusCode

public static Response.Status fromStatusCode(int statusCode)

Источник

Setting the HTTP status code

As well as sending the Servlet output to a given request, it is sometimes necessary to set a status code. An HTTP status code— sometimes called a response code— effectively tells the client what the output represents. Or in other words, it indicates the status of the requeset: successful, page not found, access denied etc.

By default, a Servlet will send send a status code «OK». Most of the time, when your Servlet has output the requested page, the default response code is the write code to send.

Occasionally, you may need to set a different status code on the response from your Servlet. To set a different HTTP status code from your Servlet, call the following method on the HttpServletResponse object passed in to your server:

where nnn is a valid HTTP status code. We’ll discuss some common HTTP status codes below.

When to set the status code

In general, you should set the status code before sending any output. This is because the status code always comes before any output in the data returned to the client. In practice, if your Servlet runner is buffering output, you may actually «get away with it» if you set the status code a bit later. But setting the status code after sending output isn’t reliable or recommended.

Always send output

Before we look at some actual status codes, it’s worth mentioning that if you set a non-default status code, you should generally send some appropriate output from your Servlet as well. The reason for this is that if you don’t, must Servlet runners send some default «system» message corresponding to the status code you set. The message may include details about your environment (such as the web server/Servlet runner version) that you don’t necessarily want the outside world to know about.

Status code constants

HTTP status codes are numeric, but for each there is generally a corresponding constant on HttpServletResponse that you can use. The constants all begin with SC_, as in the list below. So, for example, to send a status of 403 («forbidden»), you can call:

res.setStatus(HttpServletResponse.SC_FORBIDDEN);

Common status codes

The following are the most useful HTTP status codes that might be sent from a Servlet. For a more complete list, see the HTTP protocol, and the Wikipedia entry on HTTP status codes. Some of the codes not included in this list are either rarely used generally, or make very little sense from the point of view of a Servlet.

Status code 301 SC_MOVED_PERMANENTLY

Indicates a permanent redirection. Sending this code tells the client that «the location of this resource has now changed», and that the client should use a new URL in the future. If you send this code, you should also set the Location response header with the new location:

String newURL = res.encodeRedirectURL(". "); res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.setHeader("Location", newURL);

At least some search engines appear to take notice of this redirection, but of course some may not. Note the call to encodeRedirectURL() 1 , which will make sure that any request parameters are preserved.

Status code 307 SC_TEMPORARY_REDIRECT

This code is sometimes called a temporary redirect. Like the previous code, it indicates that the resource is at a different URL. A browser should instead request the provided URL, but, for example, a search engine shouldn’t permanently update its database with the new URL. A more convenient way of sending this code is to use the special method on HttpServletResponse:

String newURL = res.encodeRedirectURL(". "); res.sendRedirect(newURL);

This code is useful if you parse parts of the URL or requeset parameters in order to decide on your Servlet’s response. If the client sends parameters that are invalid in some way, you can send this response code.

Status code 403 SC_FORBIDDEN

This code may be useful for more «serious» denials of requests, for example if the user is in the wrong territory. Usually, for a «not logged in» message to the user, you would want to return a normal, «friendly» response.

Status code 404 SC_NOT_FOUND

Useful if your Servlet is dynamically interpreting the URL and returning page content accordingly. On the other hand, when the user requests a normal static page that isn’t found (i.e. when not going through your Servlet), most web servers such as Apache will automatically return the 404 response code, plus a default or customised response page.

Status code 405 SC_METHOD_NOT_ALLOWED

You could use this if, for example, you wanted to force the client to issue either GET or POST requests by returning this status code from the disallowed method (doGet() or doPost()):

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException < res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); res.getWriter().println("

Use " + "GET instead!

"); >

This code is most commonly used as part of the Java Teapot Embedded JTE TM framework. If you are not using this framework, you can generally ignore status code 418. There is currently no constant in HttpServletResponse corresponding to this code, so you should either encode it directly or use TeapotHttpConstants.STATUS_CODE_TEAPOT. For more details, see RFC 2324.

Status code 500 SC_INTERNAL_SERVER_ERROR

You could set this, for example, if you catch an exception during your Servlet code. It’s debatable whether sending this code is «giving the client too much information».

1. The version encodeRedirectUrl (note the small latters on Url) is a hangover from early versions of the Servlet framework and is now deprecated. Use the capitalised version encodeRedirectURL().

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants. Follow @BitterCoffey

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.

Источник

Оцените статью