• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to retrieve the names of the buttons in servlet ?  RSS feed

 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to retrieve the names of the buttons in servlet ?

I having 4 buttons on jsp with similar values(labels)but different names. Now I want to retrieve those names in my servlet for further manipulations.

How to do that ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All HTTP form fields are retrieved with the getParameter, getParameterNames, and getParameterValues method of HttpServletRequest.

One thing to understand is that browsers will only send the name and value of a button if it has been clicked.
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have two buttons say.........

<INPUT type="submit" name="action1" value="Add">
<INPUT type="submit" name="action2" value="Add">


In servlet if I do like this........

request.getParameter("Add");


Will it work ? It gives me "null" value.

What to do now ?
 
Anu Pillai
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<INPUT type="submit" name="action1" value="Add">


If you have a textbox like the one above, then in order to retreive its value, you have to use:

request.getParameter("action1");


If you give the attribute value="ad" in the textbox, the text 'Add' will appear inside the textbox.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
normally we use name of holding data Object to specify in getParameter(java.lang.String name) but in your case you would use getParameterNames() as ben mentioned above.. Its returns Enumeration of String objects. and its to containing name of the parameters contained in request object.

See [Java Doc]

hope it helps.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"prati",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I would have text boxes I would have retrieved the values easily via....

request.getParameter();


But I have buttons here.
So how to get the names of the buttons?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A button is an input like any other, as far as HTTP is concerned. To get its value, you need to know its name. To get the list of all parameters names -button or otherwise-, use "request.getParameterNames()"
 
NareshA WaswaniA
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For buttons, you get only that button which actually has been clicked and more over why do you want to all the names on the server side.

do one one thing, let the names of all the 4 buttons be name but give different values to them.

when someone clicks on the button, that value is passed to you which you can get on the server side by calling request.getParameter("button_name");

now since all the buttins have the name and one time you can click only one, you can make it work like this.

Any querries, shoot it back.
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You . I got the result.
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A button is an input like any other, as far as HTTP is concerned. To get its value, you need to know its name. To get the list of all parameters names -button or otherwise-, use "request.getParameterNames()"


Actually I was thinking like, "request.getParameterNames()" will work when I have same names and different values. But I have a opposite situation.

But I got the desired result through "request.getParameterNames()" only.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"prati p", David's request that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!