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....??
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?
posted 9 years ago
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?
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.
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.
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
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.
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
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.
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.