Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 675

How to determine if a parameter has been "posted" or "geted" in servlet?

$
0
0

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 therequest.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 methodused by HttpServlet?
  • PS: Here is a decoding ASP function. Shall I just rewrite it inJava?
  • 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_getdata:from_post...

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


Viewing all articles
Browse latest Browse all 675

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>