Suraj Suraj - 3 months ago 6
HTML Question

How to autochange the name attribute of button in JSP?

Hello StackOverFlowers,

I have created a table in my JSP page and the code is like this:
........

<% while(resultSet.next()){ %>
<tr>
<td><%=resultSet.getString("SN")%></td>
<td><%= resultSet.getString("fname")%></td>
<td><%= resultSet.getString("mname")%></td>
<td><%= resultSet.getString("lname")%></td>
<td><%= resultSet.getString("gender")%></td>
<td><%= resultSet.getString("email")%></td>
<td><%= resultSet.getString("phone")%></td>
<td><%= resultSet.getString("address")%></td>

<td><img src="<c:url value="DBimage.jsp"/>" width="50" height="50"></td>
<td>
<input type="submit" name ="approve" value="Approve"/>


</td>

</tr>
<% } %>


I want the name of the button to auto change.
How can I do that? Is there any easy way?

Answer

Suraj , here you go. A quick prototype that shows the concept. The value of the button is created here in the JSP itself. If you want you can compute something on the server side , send that value through response and access it through the implicit parameter response or through session for example and assign this value. For the sake of simplicity i have just used a random.

I ran this on a local jetty instance and it works!. Hope it helps

<%@ page import="java.util.Random" %>

<html>
<body>
<h2>Hello World!</h2>
<%

Random random = new Random();
int randomInt = random.nextInt(42);
String submitName="Submit" + randomInt;

%>

<input type="submit" name ="<%= submitName %>" value="<%= submitName %>"/>

</body>
</html>