Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the request method constants in the Servlet API?

Tags:

java

servlets

I want to write

if (POST.equals(req.getMethod())) 

instead of

if ("POST".equals(req.getMethod())) 

but I cannot find the constant definitions in the Servlet API (only looked in HttpServletRequest, where I expected them to be).

Where are they (I am using lots of libraries, so if someone else defines them, that would also work)?

like image 927
Thilo Avatar asked Dec 07 '09 03:12

Thilo


People also ask

What is a HTTP servlet request?

The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.

What does getServletPath do?

getServletPath. Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

What request getContentType method does?

getContentType. Returns the MIME type of the body of the request, or null if the type is not known. For HTTP servlets, same as the value of the CGI variable CONTENT_TYPE.

What is the return type of the getLastModified method of Httpservlet?

getLastModified. Returns the time the HttpServletRequest object was last modified, in milliseconds since midnight January 1, 1970 GMT. If the time is unknown, this method returns a negative number (the default).


2 Answers

It appears that Java EE 6 added the HTTP method names as constants to the javax.ws.rs.HttpMethod annotation interface. Depending on your setup, they may be available to you.

http://docs.oracle.com/javaee/6/api/javax/ws/rs/HttpMethod.html

like image 82
Matt Leidholm Avatar answered Oct 12 '22 18:10

Matt Leidholm


As far as I know, there aren't any constants for that particular property. You can check out the full list of constants to see what is available, though.

Of course, you can always define your own constants if it makes your code easier to write.

like image 40
Matt Avatar answered Oct 12 '22 18:10

Matt