Feel free to close if this doesn't really belong on StackOverflow.
javax.servlet.http.HttpServletResponse
has a bunch of useful constants (such as SC_BAD_REQUEST
, SC_NOT_FOUND
etc) but for some reason there isn't one for HTTP 429. Is there another place to find such a constant? Is there a reason it was omitted from the class?
To set a different HTTP status code from your Servlet, call the following method on the HttpServletResponse object passed in to your server: res. setStatus(nnn); where nnn is a valid HTTP status code.
All Known Implementing Classes: HttpServletResponseWrapper public abstract interface HttpServletResponse extends ServletResponse. Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.
SC_FORBIDDEN. Status code (403) indicating the server understood the request but refused to fulfill it.
ServlerResponse. isCommited() checks if the response has been already committed to the client or not (Means the servlet output stream has been opened to writing the response content). The committed response holds the HTTP Status and Headers and you can't modify it.
I don't know why they omitted it, but you can send an error with the sendError(int i, String s)
method of HttpServletResponse
. This takes an int as the first argument, so you can simply use 429
as the parameter. The second argument takes the error message. For this you could use the phrase "Too many requests".
If you are using the Spring framework then you can also use HttpStatus.TOO_MANY_REQUESTS.value()
if you really want to use a constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With