Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to perform this logic in jsp  RSS feed

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam trying to implement first,next,previous,last kind of operation in jsp page.......i wrote this logic .
it is working fine when single machine tryign to access it..
but when multiple machines tryign to acess this logic fails tell me how to implement this..
here is my logic..



[BSouther: Added UBB CODE tags]
[ July 13, 2007: Message edited by: Ben Souther ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using counter as an instance variable. Instance variables aren't thread safe, unless you make them so. Try reading up a bit on synchronization and volatile.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! int counter=0; %>

This is your problem.
As Anupam Sinha said. instance variables are not thread safe.
Rather than trying to make it thread safe, I would get rid of it and replace it either with a session scoped variable or with a marker in the page (hidden form field).
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session scoped field might not work as Abhishek Reddy Chepyala has not specified whether the counter is required for a specific session or should work for the whole application.

I am unable to get the hidden form field thing. I guess that would also be required to be stored somewhere?
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all for the reply..
yes the problem is with the instance variable counter....
some how i need to make it as a thread safe.......i tried keeping the entire thing in synchronized block......
and also tried using "isThtreadSafe" attribute...but it didnot worked

Also i need the counter to that particular session.....i dont want it for the entire application...

Also tell me is there any other way of doing it
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the hidden tag of HTML will definitely help you here.

store your variable value in hidden field and on page load get it from

request.getParameters("hiddenfieldname");

I believe that it will work.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also i need the counter to that particular session.....i dont want it for the entire application...


As you need to maintain a value for a session, you can set it on the HttpSession.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhishek Reddy Chepyala:
thanks to all for the reply..
yes the problem is with the instance variable counter....
some how i need to make it as a thread safe.......i tried keeping the entire thing in synchronized block......
and also tried using "isThtreadSafe" attribute...but it didnot worked

Also i need the counter to that particular session.....i dont want it for the entire application...

Also tell me is there any other way of doing it


None of those things will help you here.
It sounds like you're grabbing at straws which is never a good idea when coding.

An instance variable is going to be shared by all the users for that servlet/JSP. Synchronizing will only in sure that they're using it, one at a time and changing the isThreadSafe attribute will ,most likely, result in a generated servlet that implements the (deprecated) SingleTreadModel interface (new servlet for each hit).

The simplest way to implement this is to add a hidden field to the page or a querystring variable (if you're using hyperlinks instead of form submission)
that lets the servlet know which page the user is currently viewing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!