how to maintain count value over a Browser using HttpSession in java? -
i want maintain onw count value on http browser. example :
if browser once open , click on submit button make count one. again click on button make count two. , want count should less two.
consider count : private static int count & maxcount : private int maxcount = 3 code:
protected void processrequest(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); session.setattribute("count", ++count); if(session.getattribute("count") <= maxcount){ //proccess stuff }else{ //give maxcount completed message } }
it work fine when open browser first time if open browser in window showing me maxcount completed message
i recognize count static variable , gets memory once , can this. want count value again 0 when open in window of browser?
you can change this.
protected void processrequest(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); integer count = (integer)session.getattribute("count"); if( count == null) { count = 1; session.setattribute("count", count); } if(count <= maxcount){ session.setattribute("count", ++count); // other stuff. }else{ //give maxcount completed message } }
no need have static variable or private variable.
Comments
Post a Comment