views:

191

answers:

3

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)?

+2  A: 

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.

Matt
Not in javax.servlet, then. Weird that they miss the basic stuff, but have a constant of DIGEST_AUTH...
Thilo
At least they have all the HTTP status codes.
Thilo
+4  A: 

These constants are defined as private in Servlet,

public abstract class HttpServlet extends GenericServlet
    implements java.io.Serializable
{
    private static final String METHOD_DELETE = "DELETE";
    private static final String METHOD_HEAD = "HEAD";
    private static final String METHOD_GET = "GET";
    private static final String METHOD_OPTIONS = "OPTIONS";
    private static final String METHOD_POST = "POST";
    private static final String METHOD_PUT = "PUT";
    private static final String METHOD_TRACE = "TRACE";
...

It's perfectly fine just using the method name literally.

ZZ Coder
A: 

Outside of the JDK, Apache Axis has a public constant for POST (but not for any of the other methods):

org.apache.axis.transport.http.HTTPConstants.HEADER_POST

Thilo