File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Distinct names for multiple buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Distinct names for multiple buttons" Watch "Distinct names for multiple buttons" New topic

Distinct names for multiple buttons

abhinay kumar reddy

Joined: May 01, 2012
Posts: 1
Hello guys,

I have been working on a shopping site. i need to create different number of buttons in the jsp page according to the database.

this is my code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@ page import="java.sql.*" %>
<%@ page import="*" %>
<script language="javascript">


try {

String connectionURL = "jdbc:mysql://localhost:3306/login";
Connection connection = null;
Statement statement = null;
ResultSet rs= null;
connection = DriverManager.getConnection(connectionURL, "root", "root");
statement = connection.createStatement();
String QueryString = "SELECT * from revolvingchair";
rs = statement.executeQuery(QueryString);
String s[];
int i=10;
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<th>Item ID</th> <th>Item Name</th> <th>Item Description</th> <th>Item Price</th> <th>Item Available</th> <th>Quantity</th>

while ( {
<TD><input name="<% rs.getString(2);%>" type="submit" value="AddtoCart" /></TD></TR>
<% i++;
} %>
} catch (Exception ex) {

out.println("Unable to connect to database.");

<input type="submit" value="Add" name="AddtoCart" ></input>




My particular problem is i cannot differentiate the buttons when the user clicks the name wich i have given is taking as null please help me how to declare distinct names to buttons
Stefan Evans

Joined: Jul 06, 2005
Posts: 1519
Its been said many times before.
Scriptlet code in a JSP is bad
Database connections in a JSP is worse.

The Database code should be in a bean.
It should run the query, and convert the results into a List of java objects.
Your JSP should receive that List of objects and display it appropriately.

I don't see a form on your page. What are you submitting and to where?

In order to distinguish which button is pressed you need to give each button a distinct value. Or search for each button by name (the former being preferable)

I would suggest doing it as a link:

and then in the addToCart servlet handling this request:

request.getParameter("item") would tell you the id of the item you are wanting to add.

I agree. Here's the link:
subject: Distinct names for multiple buttons
It's not a secret anymore!