Forums Register Login

exercise in JSP from More Servlets

+Pie Number of slices to send: Send
Hello,

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.

Thanks in advance.
Ricky


[ May 30, 2007: Message edited by: Ricky James ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.



Hello Mirko,

Thanks for the tip. I'll try that.

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


Thanks,
+Pie Number of slices to send: Send
Thats why the request scope has got its significance. It does not keep track of the previous invocations.

Its better to have it in session or application so as to get the previously chosen color when the current request does not have the attribute set.

HtH.
+Pie Number of slices to send: Send
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?
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
https://gardener-gift.com


reply
reply
This thread has been viewed 2506 times.
Similar Threads
setting background color
About create a session!
servlet wont work
Problem with my jsp compilation/Java mail
A problem about JSP document (jspx)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:00:20.