Suraj Suraj - 1 year ago 81
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({ %>
<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>
<input type="submit" name ="approve" value="Approve"/>


<% } %>

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

Answer Source

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" %>

<h2>Hello World!</h2>

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download