• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About question 19 of Chapter 8 of Head First

 
Himai Minh
Ranch Hand
Posts: 1359
7
  • 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.
 
Greg Charles
Sheriff
Posts: 3002
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: 1359
7
  • 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: 3002
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic