Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exercise in JSP from More Servlets  RSS feed

 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!