1

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>
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
user2133606
  • 347
  • 2
  • 6
  • 15
  • Is this counter configured per session or per application start? If its per request, then you're gonna have a bad time. – Luiggi Mendoza Mar 07 '13 at 19:21
  • JSP is a server-side technology only. By the time the browser gets the HTML to render, there is no variable `x`. – GriffeyDog Mar 07 '13 at 19:22

1 Answers1

1

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:

  • You are not using a form tag
  • You name your parameter "integer" but you are trying to recover it using the name of "0"

Some guidelines for you:

  • If you want to increase the value without reloading the page you need javascript/ajax.
  • You shouldn't be adding scriptlets to your JSP pages. Insted you should code a Servlet which handles the click increase and uses a RequestDispatcher to send to a JSP page where you are showing it (and not doing any calculations).

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.

  • You should provide an answer showing what you say: *You shouldn't be adding scriptlets to your JSP pages*. By the way, I've done something similar [here](http://stackoverflow.com/a/14847751/1065197) – Luiggi Mendoza Mar 07 '13 at 19:54
  • I agree with you up to a certain extent. I could show a response similar to yours, but I think that the original poster doesn't understand the concepts behind Servlets/JSP pages and showing a code that he or she doesn't understand would serve no purpose. – Héctor Luaces Novo Mar 07 '13 at 20:02
  • Feel free to take code/text from my answer and enhance it here explaining such concepts. Our mission is to help OP (and future readers) to know and understand the whole picture, not just solving the problem using bad practices as scriptlet usage: [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Mar 07 '13 at 20:05