views:

417

answers:

5

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 nd 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:

  1. read the body with getReader...

  2. see if the xml post is there (downside, I have to manually parse the body :-( )

  3. if it is, get the http message body and get rid of the "xml=" part...

  4. if it's not, well, just get the body

  5. read the querystring parameters thru request.getParameter....

any better idea?

ps: does anybody knows how to parse the body using the same method used by HttpServlet??? ps2: here is a decoding asp function... shall I just rewrite it in java... http://www.aspnut.com/reference/encoding.asp ps3: also found http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLDecoder.html (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:


" );
    }
  }
%>

param test


  
    
    
  


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..

hey... I've entered the code between "pre" tags... but it won't show up... if you edit this question you can see the code...

+2  A: 

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

Vugluskr
+1  A: 

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?

Jason Watts
querystrings parameters are no problem, I asume I'll allways have them around... the problem is once I issue getReader, getParamters no longer returns posted values... and I was hoping to avoid parsing the post myself...
opensas
+1  A: 

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

}
Mads Hansen
you can have a post with get info, see my comment to matt b
opensas
A: 

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:

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.

matt b
If the post was made using the body of the http, instead of a parameter, I cannot get it with request.getParameter("somename"), I have to use request.getReader()... and after that I can no longer use request.getParameter
opensas
also, don't forget I can have a post with querystring info, (you just need something like 'xx.jsp?field1=value1,field2=value2' in the action parameter of the form tag), in that case, request.getMethod would return "POST", but I would have "posted" and "geted" parameters all mixed up in the getParameter collection... that's the problem...
opensas
Well I think you are using different or incorrect terminology to describe your problem then. Reading postdata or multipart data is different than reading just "parameters"
matt b
in deed I'm pretty new to java, I might confuse things a little bit... But I think that this has nothing to do with multipart stuff (I mean, the form has no enctype attribute at all)... I've updated the question to clarify things...
opensas
A: 

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

Ravi Wallau