Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to distinguish between two different button clicks on servlet

 
Bhardwaj Shweta
Ranch Hand
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 671
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following approach should also work, keep the buttons as submit button with same name



And in servlet
 
Bhardwaj Shweta
Ranch Hand
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bhardwaj Shweta
Ranch Hand
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still haven't explained what you want it to do. When does control go the the servlet and how?
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@swastik
You are absolutely right Thanks for the suggestion,i will make necessary changes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic