• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to distinguish between two different button clicks on servlet

 
Ranch Hand
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....??
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@swastik
You are absolutely right:) Thanks for the suggestion,i will make necessary changes.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic