Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

http

post

get

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 request.getParamaterMap, getParametersNames, getParameterValues etc.

Is there some way to tell which values have been posted and which ones have been specified in the URL?


PS:

What I'm trying to achieve is to make a page that can handle the following situation

  • Read the variables that came from the querystring (get)
  • Read a single post with a certain name ( "xml", for example ).
  • If that post is missing, read the whole body (with the request.getReader()).

I'm using tomcat 6.

According to what I've seen so far, if I issue a request.getReader(), posted values no longer appears in the getParamater collection, nevertheless querystring parameters are still there.

On the other hand, if I issue any of the getParameters methods, getReader returns empty string.

Seems like I can't have the cake and eat it too.

so, I guess the solution is the folowwing:

  • Read the body with getReader.
  • See if the xml post is there (downside, I have to manually parse the body.
  • If it is, get the http message body and get rid of the "xml=" part.
  • If it's not, well, just get the body.
  • Read the querystring parameters through request.getParameter

Any better idea?

  • PS: Does anybody knows how to parse the body using the same method used by HttpServlet?
  • PS: Here is a decoding ASP function. Shall I just rewrite it in Java?
  • PS: Also found (don't have a machine to test it right now)

Just to clarify things. The problem seems to be that with getParameter you get posted values as well as values passed with the URL, consider the following example:

<%@page import="java.util.*"%>
<%
  Integer i;
  String name;
  String [] values;

  for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {

    name = (String) e.nextElement();
    values = request.getParameterValues( name );

    for ( i=0; i < values.length; i ++ ) {
      out.println( name + ":" + values[i] + "<br/>" );
    }
  }
%>

<html>
<head><title>param test</title>
</head>
<body>
  <form method="post" action="http://localhost:8080/jsp_debug/param_test.jsp?data=from_get">
    <input type="text" name="data" value="from_post">
    <input type="submit" value="ok">
  </form>
</body>
</html>

the output of this code is

data:from_get
data:from_post

...

Seems like in order to find which parameter came from where, I have to check request.getQueryString.

like image 746
opensas Avatar asked Jun 17 '09 22:06

opensas


1 Answers

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.

like image 106
matt b Avatar answered Oct 15 '22 11:10

matt b