aspose file tools*
The moose likes Servlets and the fly likes recognizing different buttons pressed in html form in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "recognizing different buttons pressed in html form in servlet" Watch "recognizing different buttons pressed in html form in servlet" New topic
Author

recognizing different buttons pressed in html form in servlet

Ank Rai
Greenhorn

Joined: Jul 03, 2011
Posts: 10

hi,

m just beginning with servlets and having a confusion.

In an html form, if there are multiple submit buttons in the same form and these requires different actions to be performed then how in the corresponding servlet we'll be able to recognize which button pressed.




thanks in advance.


OCP JP 6
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5536
    
  13

you can set the name attribute of your submit buttons and check for this value in your Servlet.


<input type="submit" value="This is a submit button" name="submit1">
<input type="submit" value="Another submit button" name="submit2">
<input type="submit" value="Yet another submit button!" name="submit3">




SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Jhon Salchichon
Greenhorn

Joined: Sep 12, 2012
Posts: 10
This isn't working for me... any ideas??
In particular I have two buttons in the form:

<input type="submit" action="altaPerfilServlet" class="botonCrear" name="botonEnviar" id="botonEnviar" value="Crear perfil" onclick="validarEnviar()"
<%= request.getAttribute("esValido") != null && ((Boolean)request.getAttribute("esValido")) ? "" : "disabled" %> />

<input type=submit class="botonValidar" name="botonValidar "id="botonValidar" value="Validar datos" onclick="validarNickMail()" />

In my servlet at the beginning of the doGet method i wrote:

if(request.getParameter("botonVerificar") != null)
{
System.out.println("botonVerificar was not pressed");
}
else
{
System.out.println("botonVerificar was pressed");
}

No matter what button i press the result is: botonVerificar was pressed.


Any ideas greenhorns?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

That's because botonVerificar is defined nowhere in your HTML. So of course it's never going to be a parameter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jhon Salchichon
Greenhorn

Joined: Sep 12, 2012
Posts: 10
No words...

thanks!
Jhon Salchichon
Greenhorn

Joined: Sep 12, 2012
Posts: 10
EDIT: Problem solved.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

Have you fixed the incorrect code? If so, you need to show it. Please UseCodeTags.
Jhon Salchichon
Greenhorn

Joined: Sep 12, 2012
Posts: 10
Bear Bibeault wrote:Have you fixed the incorrect code? If so, you need to show it. Please UseCodeTags.



The correct code is:

JSP:




Servlet(doGet method):


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

Have you used any browser tools to see if the request has the expected parameters? (And please properly indent your code -- hard to read with no indentation.)
Jhon Salchichon
Greenhorn

Joined: Sep 12, 2012
Posts: 10
Bear Bibeault wrote:Have you used any browser tools to see if the request has the expected parameters? (And please properly indent your code -- hard to read with no indentation.)


Deleted all the useless information(like the scriptlet), now the code is easy to read.

No, I have not used any browser tools and I have no idea on how to do that.



Anyway, my problem now is different...

Now the form has: method='post', enctype='multipart/form-data'.
I have 3 buttons instead of 2, I had to add a javascript function to each one of them(onclick), this function validates the fields(there are text and file fields in my form) before submitting so i had to modify the buttons and set type=button(they are not submit buttons anymore because with type=submit they would call the servlet always, even if the validation was false).

The method i was using to recognize which of the submit buttons was clicked (in the servlet) was iterating over all the FileItem(s) sent with the request and if the name of any of them was the name of any of the 3 buttons in my form, then that button was the one that the user clicked. This worked perfectly when the buttons had type=submit, but now that they are simple buttons apparently they are not sent with the request, so when i get the FileItems in the servlet none of them is any of the buttons... so the trick i was using before is useless now.

I have no idea on how to do this without doing weird things like for example: adding an invisible text field in the form that is filled with the name of the button i click(using javascript)... Any better ideas?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61628
    
  67

Jhon Salchichon wrote:No, I have not used any browser tools and I have no idea on how to do that.

If you are going to be working with web apps, this is a skill you need. Now. Learn to open the JavaScript console and debuggers in whatever browsers you use.

so i had to modify the buttons and set type=button(they are not submit buttons anymore because with type=submit they would call the servlet always, even if the validation was false).

That's using a sledgehammer to crack walnuts. Simply return false from a submit hander to prevent the form from being submitted.

This worked perfectly when the buttons had type=submit,

Then that will start working again when you remove the logic to change the button types.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: recognizing different buttons pressed in html form in servlet