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
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:
getReader
.request.getParameter
Any better idea?
HttpServlet
?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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With