Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

About question 19 of Chapter 8 of Head First  RSS feed

 
Ranch Hand
Posts: 1715
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 19:


An online jewelry retailer wishes to customize their online catalog for users who are logged in. They went to show specials for the user's brithstone month. The company's special offers are stored as a Map<String, Special[]> identified as specials in application scope.
There is a bean stored as a session-scoped attribute named userInfo. Calling getBirthdate().getMonth() on this bean will return the user's birthsone month.

Which of the following code snippets could correctly retrieve the appropriate special offerings?
B.${applicationScope.specials[userInfo.birthdate.month]}



I tried to code it:




In the servlet, I have this code snippets:


The jsp is :


But the output is :


User info birthdate : 1
Special Map:


So, obviously option B does not work.
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume sc on line 17 of the second-to-last listing is the ServletContext, right? I think the problem may be that you're storing specials under the key "1" (a String), but trying to retrieve it with the key 1 (an Integer). A little debugging would probably let you see if the Map "specials" is actually stored in the application scope, and what exactly it contains.
 
Himai Minh
Ranch Hand
Posts: 1715
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I modified my code:




The relevant EL in the jsp is:


The output printed is:
Special Map: [Lmurach.business.Special;@7cba55c5
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good, that's progress! It's not totally clear to me if you're just showing that the change worked, or if you're still confused by this output. Assuming you're still confused, what you're seeing there is the "toString()" representation of the array of Specials stored in the Map under the key "1". Of course, that's not what you'd want to appear on your web page in real life. It's more likely that you'd want to display the individual offers. In that case, you'd need to iterate though the array using something like JSTL's c:forEach tag, and then get the offers for each element of the array. Something like:

 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!