Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    incrementer variable in JSP

    I am trying to put an increment variable that gets called everytime the page loads. However when i initialize the variable "int marks=0" it sticks to that value and dosent load the incremented value marks++. I tried marking the value as static int marks=0 however it gave me a compiler error. Below is the JSP code:

    Code:
     
      <%
     int marks=0;         
    String quesno=request.getParameter("quesno");
    String option=request.getParameter("option");
     String option1= request.getParameter("opt");
     if(option.equals(option1))
    {
         marks++;
    }

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    This makes sense doesn't it? You create a variable "marks" on page load, then increment it if the options are the same.

    Then you reload the page, create a variable "marks" on page load, then increment it if the options are the same.

    (etc. etc).

    Basically you are recreating this variable no page load every time, so you won't be able to set it to anything but 0 or 1.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Most proper solution for your question is save the variable value into ServletContext of web-application.

    Also you could store it in any static field of any class of your webapp.

    Tell if you need examples on any of this approaches.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •