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

前端 未结 7 1681
离开以前
离开以前 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:46

    I know it doesn't solve your problem, but if I remember correctly, when using Struts, the ActionForm will be populated if your variables are sent in the query string or in the post body.

    I found the source code for the RequestUtils class, maybe the method "populate" is what you need:

    http://svn.apache.org/repos/asf/struts/struts1/trunk/core/src/main/java/org/apache/struts/util/RequestUtils.java

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-02 04:57

    Why don't you start with checking for query string parameters, if you see none, asume a post and then dig out the form variables?

    0 讨论(0)
  • 2021-01-02 04:57

    You can use request.getMethod() to get any Method. I am using HandlerInterceptor for this.

    check this code:

    public class LoggingMethodInterceptor implements HandlerInterceptor {
    
    Logger log = LoggerFactory.getLogger(LoggingMethodInterceptor.class);
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        log.info("[START]  [" + request.getMethod() + "] [" + request.getRequestURL() + "] StartTime: {} milliseconds",
                System.currentTimeMillis());
        return true;
    }
    
    0 讨论(0)
  • 2021-01-02 05:01

    If you are writing a servlet, you can make that distinction by whether the doGet or doPost method is invoked.

    public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
          throws ServletException{
    
     //Set a variable or invoke the GET specific logic
       handleRequest(request, response);
    
    }
    
    public void doPost(HttpServletRequest request,
                        HttpServletResponse response)
          throws ServletException{
    
     //Set a variable or invoke the POST specific logic
     handleRequest(request, response);
    
    }
    
    
    public void handleRequest(HttpServletRequest request,
                        HttpServletResponse response)
          throws ServletException{
    
     //Do stuff with the request
    
    }
    
    0 讨论(0)
  • 2021-01-02 05:11

    No direct way. Non-direct - check .getQueryString()

    0 讨论(0)
提交回复
热议问题