...

View Full Version : incrementer variable in JSP



cyborg911
02-28-2012, 05:00 PM
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:



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

Aradon
03-06-2012, 10:41 PM
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.

RodionGork
03-09-2012, 02:50 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum