We help IT Professionals succeed at work.

How to trap a button click?

prain
prain asked
on
Medium Priority
203 Views
Last Modified: 2010-04-01
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
Comment
Watch Question

Commented:
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
CERTIFIED EXPERT
Top Expert 2006
Commented:
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
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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

Author

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

Author

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
Commented:
hi,

request.getParameter("show")  

should be

request.getParameter("src")

fargo

Author

Commented:
Bloodredsun,

Thanks pretty clever.

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

Thanks fargo too.
CERTIFIED EXPERT
Top Expert 2006

Commented:
cheers prain
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.