I am working on this exercise (pasted below) from the book More Servlets and JSPs by Marty Hall. Would be really thankful if somebody could advise how I should approach this exercise.
"Make a JSP page that lets the user supply a request parameter indicating the background color. If no parameter is supplied, the most recently used background color (from a previous request by ANY user) should be used."
I have attempted this exercise but don't really know how to approach the second part.
Hi, You may try to get the background parameter from the request and, if not null, set it into the application scope.Instead, if it's null, you read it from the application scope.
Originally posted by Mirko Bonasorte: Hi, You may try to get the background parameter from the request and, if not null, set it into the application scope.Instead, if it's null, you read it from the application scope.
Wont it be better if we use session to store and retrieve the information.
I tried to use request.setAttribute(...) but it didn't help, as at each request the previous kept info is removed. I am not sure about the reason. If anybody please make this clear.
The point of that exercise is to help you to understand the difference between session and context (application) scope.
Since request scope only lasts for the duration of one request, it is useless in this case.
If you bind it to session, the user will see their chosen color for the rest of their session but nobody else will be able to find out what the last color was because sessions are per user.
If you only bind the variable to application scope, UserA's colors will change as soon as UserB makes a change.
So... what to do?
Post by:autobot
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
a bit of art, as a gift, that will fit in a stocking