Reputation: 347
the application has a button and starts with the number 0, and each time the button is clicked on, the integer is increased by 1.
the problem is I have to incorporate it with this code and covert string "0" to integer 0
<%
int x = 0;
try { x = Integer.parseInt("0"); }
catch (Exception e) { x = 0; }
%>
also how would I continue to stay on the same page to click on the button to add one (do I put the code within the html?)
this is what I have so far:
<html>
<body>
<form method="post" action="index.jsp" />
<%
String integer = request.getParameter("0");
%>
<%
int x = 0;
try { x = Integer.parseInt("0"); }
catch (Exception e) { x = 0; }
%>
<input type="text" name="integer" value="<%=integer%>"/>
<input type="submit" value="submit" />
</form>
</body>
</html>
Upvotes: 1
Views: 5574
Reputation: 429
This is the code that would work for that situation.
<%! int clicks = 0; %>
<%
String param = request.getParameter("integer");
try
{
int i = Integer.parseInt(param);
clicks ++;
}
catch (NumberFormatException e)
{
}
%>
<p>Number of clicks untill now: <%= clicks %> </p>
<form action="">
<input type="text" name="integer" value="1"/>
<input type="submit" value="submit" />
</form>
You had some mistakes:
Some guidelines for you:
EDIT: I should warn you that this code would show the click value for every user on the page. If you don't want that, you should remove de JSP declaration and work with the param request instead. From my code it should be easy for you.
Upvotes: 1