Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.servlet.http.HttpServletResponse SC_TOO_MANY_REQUESTS

Tags:

java

http

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?

like image 756
akhaku Avatar asked Jun 11 '18 05:06

akhaku


People also ask

How do I change the response status code in Java?

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.

What is the HttpServletResponse?

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.

Which of these is the HTTP response code when there is a resource Forbidden error HttpServletResponse Sc_forbidden?

SC_FORBIDDEN. Status code (403) indicating the server understood the request but refused to fulfill it.

How do I know if a response is committed?

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.


1 Answers

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.

like image 132
Maurice Avatar answered Oct 03 '22 08:10

Maurice