How to determine if a parameter has been “posted” or “geted” from Java?

前端 未结 7 1737
离开以前
离开以前 2021-01-02 04:32

In ASP, there\'s request.form and request.queryString attributes, but in Java. It seems like we have only one collection, which can be accessed via

7条回答
  •  情书的邮戳
    2021-01-02 04:53

    HttpServletRequest.getMethod():

    Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.

    All you need to do is this:

    boolean isPost = "POST".equals(request.getMethod());
    

    Also I'm really confused on why you wouldn't simply use request.getParameter("somename") to retrieve values sent as request parameters. This method returns the parameter regardless of whether the request was sent via a GET or a POST:

    Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

    It's a heck of a lot simpler than trying to parse getQueryString() yourself.

提交回复
热议问题