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
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
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.
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?
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;
}
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
}
No direct way. Non-direct - check .getQueryString()