How to know which button is clicked?

Hi, I am new to JSP and struts.

I have two button, btnAdd and btnDelete on the page, and the form will be passing to the servlet "HdlButtonAction" to handle the button click

1)Using JSP without struts, what is the common way to know which button is clicked?
2)Using JSP with struts, what is the common way to know which button is clicked?
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KuldeepchaturvediCommented:
you can do request.getParamter(buttonName)... the clicked butten will return a not null value...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leogcCommented:
u have to use javascript code for both i.e.
document.forms[formname].btnAdd.value and document.forms[formname].btnDelete.value.Write onclick event for the butons and call a method.
For struts u r action class can extend DispatchAction class or LookUpDispatchAction class.
0
TimYatesCommented:
>> u have to use javascript code for both i.e.

No you don't

http://husted.com/struts/tips/003.html
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

fargoCommented:
1)Using JSP without struts, what is the common way to know which button is clicked?
2)Using JSP with struts, what is the common way to know which button is clicked?

In both the cases, u don't need to have javascript handling.

Just define different submit button with same property but different value. Like as follows

<html:form ...>
...
<html:submit property="action" alt="Submit" value="Submit"></html:submit>
<html:submit property="action" alt="CheckOut" value="CheckOut"></html:submit>
</html:form>

same in html form without struts
<input type="submit" name="action" value="Submit" alt="Submit">
...

In the action class, you can get the value of the button as

String clicked = request.getParamter("action");

0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
yes i agree with Fargo this an efficent way and also you can resolve the button clicked by using java script too.

In which they generally use hidden parameters for resolving the button clicked, this method is useful when more than one submit buttons are there in the form.

Thank You.
0
TimYatesCommented:
>> In which they generally use hidden parameters for resolving the button clicked, this method is useful when more than one submit buttons are there in the form.

Or you can just do it like I said here: http:Q_21812228.html#16444083
0
fargoCommented:
>>In which they generally use hidden parameters for resolving the button clicked, this method is useful when more than one submit buttons are there in the form.

not required to have javascript or hidden params even if u have multiple submit buttons. The example i gave says it all.
0
JoeCommented:
I agree with fargo.  No need for the JS.  Just do a request param for the button name.  You can also name all of the buttons the same name and the browser will pass the value which was on the button.  

In Struts just make a property in your form bean for the button and it will get set into there.  They you can check to see what button was clicked

<input type="submit" name="mybutton" value="SAVE" />
<input type="submit" name="mybutton" value="CANCEL" />

then in your Struts code

if (getMybutton().equals("SAVE"){
//do something....

}

or if not using Struts you can get it through the request as fargo suggested

-Joe
0
TimYatesCommented:
Joe, you're repeating what has been said at least twice here...

Thanks all the same...
0
jessegivyDeveloperCommented:
Oooooo, are the experts getting a little testy?  I think Joe was just trying to avoid the askers confusion what with all this JS talk in the JSP TA.  Anyway, the first answer is indeed correct from Kuldeepchaturvedi,  not sure what the discussion is about? ;)

I use conditions like this quite often when it's just the button I care about:

if(request.getParameter("buttonName")!=null)
{
   //insert,update,do whatever appropriate for
   //buttonName being clicked
}

cheers,

~Jesse
0
william007Author Commented:
Thanks:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.