user2133606
user2133606

Reputation: 347

How can I add one or increment the integer each time I hit the submit button?

jsp file

<html>
<body>
<form method="post" action="index.jsp" />

<% 
String integer = request.getParameter("integer"); 
%>

<%
int x = 0;
try { x = Integer.parseInt("integer"); }
catch (Exception e) { x = 0; }
%>

<input type="text" name="integer" value="<%=x%>"/>
<input type="submit" value="submit" />

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

how would I add or increment x the output each time when I hit the submit button?

Upvotes: 0

Views: 2462

Answers (2)

Joseph Caracuel
Joseph Caracuel

Reputation: 974

Integer.parseInt("integer"); -> "integer" string is not a number therefore it will be a wrong format.

and you are initializing x = 0 every time and there is no increment on x

you could try this:

<form method="post" action="" />

<%
  String integer = request.getParameter("integer");          
  int x = integer != null ? Integer.parseInt(integer) : 0;      
  ++x;
%>

<input type="text" name="integer" value="<%=x%>"/>
<input type="submit" value="submit" />

Upvotes: 1

Vetsin
Vetsin

Reputation: 2592

<html>
<body>
<form method="post" action="index.jsp" />

<% 
String integer = request.getParameter("integer"); 
%>

<%
int x = 0;
try { x = Integer.parseInt("integer"); }
catch (Exception e) { x = 0; }
x = x + 1;
%>

<input type="text" name="integer" value="<%=x%>"/>
<input type="submit" value="submit" />

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

Upvotes: 0

Related Questions