• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Create thread safe JSP page

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I was wondering if there is any code to add to my jsp page to make sure that 2 different people viewing the page see different pages. ie. To ensure that a new instance of the jsp page is sent to every different browser on which the page is viewed. Is this possible?
Thanks.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need a new instance?

JSPs are set up such that there is one instance of the servlet per container.
Each request is handled in a separate thread.
If you avoid the use of instance variables, (declare all of you variables from within the service method (the <% ... %> tags). Your JSPs should be thread-safe.



[further]
There are other ways that you could write non-thread-safe code.
One way would be to share values stored in session or context scope.
This is something to watch for regardless of how you write your JSPs (single thread or otherwise).
[ August 30, 2005: Message edited by: Ben Souther ]
 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Problem I actually have is this:
The variable is being declared in the <%! %> tags.
A button is clicked on the page, and after loading, the value of this variable should still be displayed on the page. You know that after the page resubmits to itself, it is as if a new page has been loaded.

So I'm looking for a way to resubmit the page to itself and still be able to display the last value of the variable.
So far I am only able to achieve this by declaring the variable in the <%! %> tag. The problem is that the variable has the same value on every other browser on my Local Area network even though the page has not been resubmitted.
Thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables declared in the "<%! ... %>" tags are instance variables and are not thread safe. If you did create a single thread JSP (which you shouldn't) you would still not get the desired result because each instance would create a new copy of the variable (which means that the value would not carry over).

If you need to access a value across requests, bind it to either session or context scope. You may need to add a synchronized block to insure thread safety.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest that you initialize your variable every time it resubmits to itself.

What I mean is just before submitting the page or the last time the variable value changes store it as a request or pageContext object i.e. Integer. request.setAttribute("myVar", (Integer)myVar) and then when the page reloads just get the last value e.g. int myVar = request.getAttribute("myVar")...

This is one way to go.. there are plenty of other options.
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would suggest that you initialize your variable every time it resubmits to itself.

What I mean is just before submitting the page or the last time the variable value changes store it as a request or pageContext object i.e. Integer. request.setAttribute("myVar", (Integer)myVar) and then when the page reloads just get the last value e.g. int myVar = request.getAttribute("myVar")...



I don't see how this can be done, since submission happens on the client...

Personally, I would save the variable in the Session. Use session.setAttribute() and session.getAttribute(). If all you're doing is storing Strings or primitive wrappers, you don't even need to synchronize anything.

If you want the variable to persist after the client closes the browser, you need to save it in the servlet context (application.setAttribute() etc). If you want the variable to persist between server reboots, you need to use a cookie.

-Yuriy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic