wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes How to distinguish between two different button clicks on servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to distinguish between two different button clicks on servlet" Watch "How to distinguish between two different button clicks on servlet" New topic
Author

How to distinguish between two different button clicks on servlet

Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

I have three Buttons
1.Add(type="button") and on click of this button control remains on the same jsp page.
2.Edit(type="button") and on click of this button control remains on the same jsp page.
3.Save(type="submit") and control goes to servlet.

So my query is after clicking save how would i check in servlet wether before save,button add or edit was clicked....??

Regards,
Shweta Bhardwaj
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

You could add a hidden field, and use Javascript event handler on the buttons to set its value.

[Moving thread to another forum - this isn't servlet specific]
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 539

If you are looking to process only add or edit at one time, then instead of making add and edit as buttons, i suggest to make them as radio buttons, so that you get which radio button is clicked in the servlet.

P:S: In the current scenario, if the user first clicks add and then edit, which would you take as the operation to be done. Add or Edit?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

I must admit, it seems a bit counter intuitive to press two buttons like that. But it depends on how the form works - maybe you could explain that, we might be able to suggest a better approach?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Following approach should also work, keep the buttons as submit button with same name



And in servlet


Swastik
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

Thanks for your replies..
@swastik I cannot make these buttons of submit type because i want to retain the control on the same jsp page..


i have two functionality in save button.
first case:
1.user clicks add button to insert new records--->clear all the textboxes and enables save and cancel button.
2.user can either click save or cancel.

second case:
1.user clicks edit button to edit the existing record.
2.either save or cancel


now i am not getting how to identify which button has been clicked..need to replace this ### in if condition
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Bhardwaj Shweta wrote:@swastik I cannot make these buttons of submit type

Swastik has the correct approach.

because i want to retain the control on the same jsp page..

You'll have to explain what you mean by that. Does it mean you are using Ajax? If so, you can still use submit buttons. Do not reject a correct approach simply because you do not understand the environment in which you are working.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

oops!!! i sincerely apologize for that.
i am not using ajax, the problem with submit type is if i make add (of submit type)the moment i press add, control goes to servlet but i dont want that.
So button type seems more appropriate
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

You still haven't explained what you want it to do. When does control go the the servlet and how?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

@Shweta

As far as I understood your problem, you might apply the following logic. Let your Add and Edit buttons be normal buttons. On click trigger a java script event, that enables save(this is of type submit) and cancel, clear the text fields as you have mentioned, and sets a value in some hidden field as suggested by Matthew. Once save is clicked, data is submitted to servlet. Now in servlet apart from retrieving other field values, retrieve the value of hidden field and apply the insert or update logic.

Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

Hey swastik Thanks for this great understanding and encouraging me
i tried doing this by using hidden fields and problem is fixed


//servlet
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Shweta,



A small suggestion regarding the above code. What you have written is absolutely fine. But what happens when due to some reason ch holds a null reference? It will throw a NullPointerException. So it's always advisable to compare the constant with the variable, in this way you will never get a NPE.

Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

@swastik
You are absolutely right Thanks for the suggestion,i will make necessary changes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to distinguish between two different button clicks on servlet