Forums Register Login

Create thread safe JSP page

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2120 times.
Similar Threads
getting data from html drop down list to jsp
jsp calls 1 of 2 servlets
how to invoke tiles definition from JSP
one jsp to another
how to distinguis links on a jsp ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:57:21.