Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to know which button is clicked?

Posted on 2006-04-12
11
Medium Priority
?
2,158 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:william007
11 Comments
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1000 total points
ID: 16442704
you can do request.getParamter(buttonName)... the clicked butten will return a not null value...
0
 
LVL 2

Expert Comment

by:leogc
ID: 16442723
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
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 400 total points
ID: 16444083
>> u have to use javascript code for both i.e.

No you don't

http://husted.com/struts/tips/003.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Assisted Solution

by:fargo
fargo earned 600 total points
ID: 16444091
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
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16447458
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
 
LVL 35

Expert Comment

by:TimYates
ID: 16448350
>> 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
 
LVL 11

Expert Comment

by:fargo
ID: 16448368
>>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
 
LVL 16

Expert Comment

by:Joe
ID: 16452245
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
 
LVL 35

Expert Comment

by:TimYates
ID: 16458439
Joe, you're repeating what has been said at least twice here...

Thanks all the same...
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 16458978
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
 
LVL 9

Author Comment

by:william007
ID: 16464292
Thanks:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The number of companies understanding the potential of IoT on B2B market is growing with each day. And yet only a small share of IoT developers have managed to equalize incomes and stay competitive in the international market.
As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a user’s mailbox on Exchange Server. Though, if the server’s status is changed or it is dama…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question