aspose file tools*
The moose likes JSP and the fly likes Question on BUTTON name/value pairs sent from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Question on BUTTON name/value pairs sent from JSP" Watch "Question on BUTTON name/value pairs sent from JSP" New topic
Author

Question on BUTTON name/value pairs sent from JSP

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
I have a very simple JSP where I have one button that sends the name/value pair "mainbutton"/"new". Here's the code snippet:

<button type=submit name="mainbutton" value="new">Create New Issue</button>

When this button is selected, my servlet calls the
request.getParameter("mainbutton") method and when I print the resulting string, I get "Create New Issue"; I was expecting "new" to be returned.

Am I missing something?

Thanks,
Scott
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
Can't you set the value of a hidden field on button click and call getParameter() of hidden field name ? Why are you trying to get the value from button ? The purpose of button is not to store values

Thanks,
Rajeev
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Originally posted by Rajeev Ravindran:
The purpose of button is not to store values


Nobody is storing anything. The button tag supports the name and value attributes which are submitted when the button is clicked. Nothing wrong with using that.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Hmmmm.

I set up the following test html:



results.jsp is a page that displays the values of the request parameters via:



What I get upon clicking button 2:


Post Results

Request Parameters

xyz=XYZ
button=value2



So for me, everything is working as expected.
[ February 24, 2006: Message edited by: Bear Bibeault ]
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Ok, here's another issue along the same lines. Here's my intial JSP file (please accept my apologies, I'm somewhat new to JSP coding as you'll see):

--------

<%@ page import="java.util.*,java.sql.*" %>

<html>
<body>
<h1 align="center"><B>BA Issues</B></h1>
<p>
<form method="post"
action="General.do">
<%

ResultSet rslts = (ResultSet) request.getAttribute("results");
out.println("<table BORDER=3><TR><TH>Issue Name</TH><TH>Priority</TH></TR>");
while (rslts.next()) {
String name = rslts.getString("issue_name");
String priority = rslts.getString("priority");
out.println("<TR><TD>"+name+"</TD><TD>"+priority+"</TD></TR>");
}
out.println("</table>");
%>
<button type="submit" name="button" value="new">Create New Issue</button>
<button type="submit" name="button" value="delete">Delete Issue</button>

</form>
</body>
</html>


-----

Regardless of which of the two buttons I select, my servlet returns the following values for the String value of request.getParameter("button"):

"Create New Issue"

(Also, if I switch the order of the two buttons, then my servlet will always return "Delete Issue".) Ugh.

Thanks,
Scott
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Try giving your buttons different names.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Originally posted by Paul Clapham:
Try giving your buttons different names.


But that would make it needlessly difficult to determine which had been pressed.

This is mystifying me since it works fine in my environment (Tomcat 5.5, Mac OS X and Windows XP, Mozilla 1.5 and IE 6).
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
That works, but then the servlet doesn't know which name/value pair was actually selected in the form - they all come back as being returned when I ask for the parameter based on name.

Again, thanks to everyone who's replied!
Scott
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Originally posted by Scott Updike:
I'm somewhat new to JSP coding as you'll see


Then here are a few comments and suggestions:

1) Move your Java logic out of the JSP page and into servlets and/or beans. This is escpecially true of JDBC code.

2) Why are you emitting HTML markup from Java? You're already in a JSP page. Building up markup in Java strings is just plain weird.

With regards to your issue. My next suggestion is to do a View Source of the page containing the buttons once it hits the browser to make sure that the HTML markup is well-formed and what you expect it to be.

Post it here (please use UBB code tags), if you like, for inspection.
[ February 24, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

You might also want to try the sample code I posted to see if it works as expected. If it does, that detemrines that the issue definitely lies with your page.
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
I'm getting fairly frustrated since it's preventing me from doing other things. I'm running WinXP, Tomcat 5.5. I promise this is a very basic environment! If it helps, I'm using the above JSP calling the following servlet:

------

package com.baissue;


import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;



public class general extends HttpServlet {

public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String buttonValue = request.getParameter("button");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("ButtonValue = " + buttonValue);

}

} //end class


I promise; this is a very basic program.
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Well, like I said before, I'm just starting to learn JSPs and "I am where I am" in the books. So far, they're still using HTML in the JSPs at this point.

The returned values via my JDBC connection are returning fine. It's only the buttons that I'm having trouble with. I can certainly move the JDBC code out of the JSP layer and back into the Servlet layer.

I did follow your advice and did a "view Source" on the web page and it looks fine (as far as I can tell):

---------------------

<html>
<body>
<h1 align="center"><B>BA Issues</B></h1>
<p>
<form method="post"
action="General.do">
<table BORDER=3><TR><TH>Issue Name</TH><TH>Priority</TH></TR>
<TR><TD>Issue1</TD><TD>1</TD></TR>
<TR><TD>Issue2</TD><TD>2</TD></TR>
<TR><TD>Issue3</TD><TD>3</TD></TR>
<TR><TD>Issue4</TD><TD>4</TD></TR>
</table>


<p><B>Issue Name:</B><input type=text name="issue" size=20 maxlength=20></p>
<button type="submit" name="button" value="delete">Delete Issue</button>
<button type="submit" name="button" value="new">Create New Issue</button>

</form>

</body>
</html>

------------------
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Originally posted by Paul Clapham:
Try giving your buttons different names.


Paul, did you mean to give the buttons different name from each other? Or to just avoid using the term 'button'?

I assumed the former, but on re-reading...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

HTML looks fine (although I'd be sure to quote all attribute values for safety) upon a quick inspection.

At this point I suggest running my sample code to see if you get the same results as I do,
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Will do. Thanks again for your help.
Scott
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Well, as expected, your code worked. Still not sure why mine does not.

I'll try and jump ahead and figure out your syntax and keep groov'n.

If you have any other ideas, let me know!

Thanks again.
Scott
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

More info:

I took your HTML, plunked onto my server and modified the action of the form to be my results.jsp file and it worked as exepcted.
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Boy, I didn't need to see that...

Could this be a configuration issue in Tomcat?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Doubt it.

Try submitting your form to results.jsp and see what you get...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

I doubt it's a browser issue either... but just to eliminate that, also try it in a different browser.
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
It seems like the more you know, the more complicated sometimes...

If you just change the form method to GET instead of POST, you will see what parameters get attached to the url when form is submitted.

I guess the problem will be solved if you use the HTML 'input' element with type submit. In this case only the information of the button clicked will be attached to the form data when submitting even if the form has multiple submit buttons with the same name. While the 'Button' element you are struggling with has some advantages like decorating the button label more nicely, it was previously supported by Internet Explorer only (not sure other browsers now support it).

I didn't try but it looks like with the 'Button' element you can send multiple values associated with the same parameter name. In this case you can get all the values using request.getParameterValues("your_param_name") as you may already know.

Hope this helps.
[ February 24, 2006: Message edited by: Heonkoo Lee ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

not sure other browsers now support it


All modern browsers of note support the button tag.
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Ok. Based on some of the suggestions, I can get my multiple buttons to work properly if I use the input tag as follows:

<input type="SUBMIT" name="mainbutton" value="new">
<input type="SUBMIT" name="mainbutton" value="delete">

Still not sure why using the plain old BUTTON tag does not work, but this will get me back on my learning track. I thank everyone again for your suggestions.

Thanks!
Scott
Jason Rusty
Greenhorn

Joined: Feb 28, 2006
Posts: 1
It looks as though IE 6.0 and Firfox 1.5 handle the button tag slightly differently. I got different output with the following code:

File: Hello.jsp



File: request.jsp



IE results in:

You selected:
button Button 1
button Button 2
button Button 3

Firefox results in:

You selected:
button value2

However, this code works on both IE and Firefox if you change the button tag to
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on BUTTON name/value pairs sent from JSP