How to trap a button click?

Hello,

Here is the scenerio....

I have two buttons showing on browser. Both buttons are linked to the same Servlet.

How do I trap which one was clicked by the user WITHIN  THE doPost() method?

Thanks
prain
prainAsked:
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.

fargoCommented:
Hi,

One way is to use a hidden parameter to see which button is click.

You can set the value of the hidden parameter depends upon which button is clicked.

fargo
0
bloodredsunCommented:
add a parameter to the link so that one button goes to "http://www.myhost.com/myservlet.do?src=button1" and the other goes to "http://www.myhost.com/myservlet.do?src=button2"

THen in the servlet code call this

if("button1".equals( request.getParameter("src") ){
//send by button1
}else{
//sent by button2
}
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
fargoCommented:
Hi,

<script>
 
function update(valuetoset){
document.forms[0].hiddenparamname.value=valuetoset;
document.forms[0].submit();
}

</script>

<html:button.......onclick="update('button1value');"/>

<html:button.......onclick="update('button2value');"/>

fargo
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

prainAuthor Commented:
bloodredsun,

I like your method. But when I implemented that in my source, I am getting the famous HTTP error 404. It does not recognise the .do part?

Here is a portion of my code...

out.println("<form method=\"post\" action=\"/pSystem/PFolders.do?src=show\">");
.....
....
out.println("<form method=\"post\" action=\"/pSystem/PFolders.do?src=add\">");


and then in my doPost() I have a block like this....

if("show".equals( request.getParameter("show") ) ){
  System.out.println("coming from show");
}else{
System.out.println("coming from add");
}

And the error is
HTTP Status 404 - /pSystem/PFolders.do

--------------------------------------------------------------------------------

type Status report

message /pSystem/PFolders.do

description The requested resource (/pSystem/PFolders.do) is not available.


Note : I have a PFolders.class file in the appropriate folder in the WEB-INF and is a working Servlet and
I have been working with that for a while now. Only thing I added was the .do  as you have specified.

Any input will be appreciated.

Thanks
prain
0
prainAuthor Commented:
OK. Blood,

I think I fixed that problem.

But now I have another problem. When any of the two buttons is clicked,
it always goes to "Coming from add".

Trying to figure out.

-prain
0
fargoCommented:
hi,

request.getParameter("show")  

should be

request.getParameter("src")

fargo
0
prainAuthor Commented:
Bloodredsun,

Thanks pretty clever.

I will split the points b/w blood and fargo, but majority goes to Blood.

Thanks fargo too.
0
bloodredsunCommented:
cheers prain
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.