• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFS Mock Qn Error

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Head first Servlet Chap 9 Mock Qn7, I guess all the answers are wrong. The question is listed here in case you dont have the book:

For this scriptlet (some are omitted):
...
<%BeerStyle[] styles=beerService.getStyles();
for (int i=0;i<styles.length;i++) {
BeerStyle style=styles[i]; %>
...
<%}%>

The equivalent JSTL is say to be option B:
<c:forEach var='style' items='${beerService.styles}'>
...
</c:forEach>

The details are not listed. The important thing I want to ask is 'beerService' is a java scriptlet variable - not attribute nor implicit object. Can we use ${beerService.styles} to retrieve its content? I think there is no way to retrieve a scriptlet variable by EL directly.
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I could be wrong here cos I don't have the book with me to see the whole question, howevere it does not appear to me that
'beerService' is a java scriptlet variable
BeerStyle[] styles is a java scriptlet variable, but it is not clear from what you posted where beerService comes from. So why could it not be an attribute somewhere?

Mat
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mat,

See the line "beerService.getStyles();". That means it is not an attribute. How "beerService" is declared is not given but I dont think we can say the two versions of code are equivalent unless an attribute name "beerService" is explicitly declared. But the book's question does not do so.
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alec,

The line 'beerService.getStyles();' does not prove that beerService is not an attribute that has been obtained higher up in the jsp through some other scriptlet. beerService is just a variable, how it gets set, or what the datatype is, is not specified, and in my reading of the question (if you have provided the whole question) it is not considered relevant. I think what the Authors are really saying is, given a variable called beerService, that has a method getStyles() which returns an array of BeerStyle objects, is option B equivalent to the scriptlet code provided.

Mat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic