File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How can i find out, from where my parameter coming from? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can i find out, from where my parameter coming from?" Watch "How can i find out, from where my parameter coming from?" New topic
Author

How can i find out, from where my parameter coming from?

Arthur Purnama
Greenhorn

Joined: Sep 02, 2008
Posts: 4
Hello, im really new here, and im playing around with servlets.

I hope i got the right title topic for my question. I didn't find the answer in javaranch nor in google. maybe because i didn't type in the right search key. but anyway. here is my question.

i have a simple jsp file. named formtest.jsp. it looks like this.


as you can see, in the action attributes i give the address to "MyFirstServlets" servlet and i give a get parameter "?blah=blub&bleh=blob".
and i create 2 text field which have "blah" and "bleh" as a name. the same name as in the get parameter. i do this intentionally because i want to learn how to find out later where my parameter values coming from.

my "MyFirstServlets" looks like this:



now i go to my jsp and run it on server. type "foo" in my first text field and "bar" in the second one. and klick on submit. and i got this result on my browser:



as you can see the servlet container go to the doPost() method. but print the GET value. i can modified my doPost method and use the method getParameterValues like this:



and i got this result print out to my browser:




as you can see, it show me the values from GET and from POST altogether, but i still can not find out, from where the value come from. i want to know that "foo" and "bar" coming from the POST and "blub" and "blob" coming from the GET.
Is there any possibility that i can do about this?

thank you.


MfG<br />Arthur Purnama
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to javaranch . We are happy to have you here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Coming to your question ,suppose you have more than one input parameter with same name

then you will get first param value in servlet if you get the value using getParameter . to get all the values use getParameterValues()

to clear understand make form method to get and then submit ,also importantly wath the url bar of the browser (the sequence of parameter)

Hope this helps
Arthur Purnama
Greenhorn

Joined: Sep 02, 2008
Posts: 4
seetharaman venkatasamy wrote:Coming to your question ,suppose you have more than one input parameter with same name

then you will get first param value in servlet if you get the value using getParameter . to get all the values use getParameterValues()

to clear understand make form method to get and then submit ,also importantly wath the url bar of the browser (the sequence of parameter)

Hope this helps


Thank you for your answer. but the example i give above is because i want to know both information at the same time. of course if i change the method to get then all information will be come from get, and the servlet container will go to the doGet method instead of doPost. but thats not the point.

the point is. if you have situation like i describe above. that means. you have parameters from GET and POST with the same name how can you differentiate that a parameter "blah" with value "foo" is coming from the POST and a parameter blah with value "blub" is coming from GET at the same time in the doPost() method?

of course i can get all the values using getParameterValues. i give it at my example too. but you can still didnt know which parameter come from POST or GET.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what do you mean by coming from post and get?

Clearly,

suppose in your form using GET , action attribute has parameter like actionServlet? name = Merrill and also your input parameter has same name like <input type=text name="name"/> then when you submit input parameter override the query string param. because Get method carry the input parameters in Query string

In Post , you will get both the values beause post carry the input parameter in body insteadof url(QueryString).

so when you use getParameterValues , you get both .but it wont happen if you use GET method in your form
Arthur Purnama
Greenhorn

Joined: Sep 02, 2008
Posts: 4
Hallo,

Thanks for the answer.

you said:

so when you use getParameterValues , you get both .but it wont happen if you use GET method in your form


thats right. if i use getParameterValues, i get both. but my question will be. how can i tell the difference, which one come from the "body" and which one come from the "url(QueryString)" ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The only way to tell would be to parse the query string yourself and read the values contained within it.
Even then, if there are multiple parameters with the same name in the form post it will be difficult to know for certain.

Maybe a better question would be, "Why do you need to know this?".
Your code would be a lot clearer if you didn't send querystring and post parameters at the same time.
If you're making a post request, keep everything in the post string.
When making get requests, keep everything in the query string.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Arthur Purnama wrote:which one come from the "body" and which one come from the "url(QueryString)" ?


form input parameters all are coming from body of the post. and your action attribute append values are from queryString
Arthur Purnama
Greenhorn

Joined: Sep 02, 2008
Posts: 4
I Thank you both for your answer.

i will answer it for you both.

@seetharaman venkatasamy
form input parameters all are coming from body of the post. and your action attribute append values are from queryString


yes i know that ^^. but thats not answer my question. how to find out "in code" what you need to write in your code to tell that it coming from different resource. i try to make a code for example. i hope that it make you understand what i mean and what i want to know .



do you know how to complete this part



so that the result come like this



make no change in the formtest.jsp thats the idea.

@Ben Souther
I know somebody will asking me "why i would do this". my answer will be. im in learning phase right now. and this fact is really annoying for me. if i working some other time in a project. i will not be working alone. and in a team, you need to expect the worst case that can happen, and trying to prevent that. if i can for sure know that my code only read the data coming from post or only from get. i can make my application more robust.

and it is common in web application that you have a GET parameter and POST parameter at the same time. That they have coincidentally the same name can be nearly impossible, it is still not impossible. people make mistake . its humane. im doing this because im learning servlet right now. i just want to be prepare to have this kind of situation.

i used to write web application with PHP. in PHP you have 2 different global variabel named $_GET and $_POST in which consist of array. the code above will be resolved in PHP like this.



What for me really interesting in this case is that, even im going to the doPost() method. the request.getParameter("blah"); is showing me the value from GET which is "blub". i expect it that it will give me the value from POST which is "foo". this is really absurd for me. i think it is clearly for everybody that there is parameter that goes in url and parameter that goes in the body. there should be a way to access it separately shouldn't it?

i think from your answer, there is no way to access it separately with the API. i need to parse the get string and the post string manually.

thank you once again.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

With servlets, your querystring and post parameters are mixed together.
There is no method in HttpServletRequest to discern which was originally part of the querystring and which was a post parameter.

Does this help?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

And by the way, you do not a mix of GET and POST. All the request parameters are conveyed on a single HTTP request which is either a GET or a POST, but never both.

Thinking of parameters on the query string as GET parameters will just lead you to confusion and pain. Stop thinking that way


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i find out, from where my parameter coming from?