aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Distinct names for multiple buttons" Watch "Distinct names for multiple buttons" New topic
Author

Distinct names for multiple buttons

abhinay kumar reddy
Greenhorn

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"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<script language="javascript">

</script>

<title>Solitude:Furniture-3</title>
</head>
<body>
<%
try {

String connectionURL = "jdbc:mysql://localhost:3306/login";
Connection connection = null;
Statement statement = null;
ResultSet rs= null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
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;">
<thead>
<th>Item ID</th> <th>Item Name</th> <th>Item Description</th> <th>Item Price</th> <th>Item Available</th> <th>Quantity</th>
</thead>
<%

while (rs.next()) {
%>
<TR>
<TD><%=rs.getInt(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><%=rs.getString(3)%></TD>
<TD><%=rs.getInt(4)%></TD>
<TD><%=rs.getInt(5)%></TD>
<TD><input name="<% rs.getString(2);%>" type="submit" value="AddtoCart" /></TD></TR>
<% i++;
} %>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {


out.println("Unable to connect to database.");
}
%>

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

</TABLE><TABLE>
<TR>

</TR>
</TABLE>
</form>
</body>
</html>

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

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
Bartender

Joined: Jul 06, 2005
Posts: 1016
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: http://aspose.com/file-tools
 
subject: Distinct names for multiple buttons
 
Similar Threads
jsp not showing result in table form
Access denied for user
problem in select
retreiving all rows from a mysql table to a jsp page
JSP DataBase Connection