Coffee Cram: Chapter 9 Answers

  1. Which is true about TLD files?

    (JSP v2.0 pgs 3-16, 1-160)

    A.

    TLD files may be placed in any subdirectory of WEB-INF.

    B.

    TLD files are used to configure JSP environment attributes, such as scripting-invalid.

    C.

    TLD files may be placed in the META-INF directory of the WAR file.

    D.

    TLD files can declare both Simple and Classic tags, but TLD files are NOT used to declare Tag Files.

    Note

    -Option B is invalid because TLD files configure tag handlers not the JSP environment.

    -Option C is invalid because TLD files are not recognized in the META-INF of the WAR file.

    -Option D is invalid because Tag Files may be declared in a TLD (but it is rare).

  2. Assuming the standard JSTL prefix conventions are used, which JSTL tags would you use to iterate over a collection of objects? (Choose all that apply.)

    (JSTL v1.1 pg. 42)

    A.

    <x:forEach>

    B.

    <c:iterate>

    C.

    <c:forEach>

    D.

    <logic:iterate>

    E.

    <logic:forEach>

    Note

    -Option A is incorrect as this is the tag used for iterating over XPath expressions.

    -Option B is incorrect because no such tag exists.

    -Options D and E are incorrect because the prefix ‘logic’ is not a standard JSTL prefix (this prefix is typically used by tags in the Jakarta Struts package).

  3. A JSP page contains a taglib directive whose uri attribute has the value myTags. Which deployment descriptor element defines the associated TLD?

    (JSP v2.0 pgs 3-12,13)

    A.

    <taglib>
      <uri>myTags</uri>
      <location>/WEB-INF/myTags.tld</location>
    </taglib>

    B.

    <taglib>
      <uri>myTags</uri>
      <tld-location>/WEB-INF/myTags.tld</tld-location>
    </taglib>

    C.

    <taglib>
      <tld-uri>myTags</tld-uri>
      <tld-location>/WEB-INF/myTags.tld</tld-location>
    </taglib>

    D.

    <taglib>
      <taglib-uri>myTags</taglib-uri>
      <taglib-location>/WEB-INF/myTags.tld</taglib-location>
    </taglib>

    Note

    - Option D specifies valid tag elements.

  4. A JavaBean Person has a property called address. The value of this property is another JavaBean Address with the following string properties: street1, street2, city, stateCode and zipCode. A controller servlet creates a session-scoped attribute called customer that is an instance of the Person bean.

    (JSTL v1.1 pg 4-28)

    Which JSP code structures will set the city property of the customer attribute to the city request parameter? (Choose all that apply.)

    A.

    ${sessionScope.customer.address.city = param.city}

    B.

    <c:set target="${sessionScope.customer.address}"
           property="city" value="${param.city}" />

    C.

    <c:set scope="session" var="${customer.address}"
           property="city" value="${param.city}" />

    D.

    <c:set target="${sessionScope.customer.address}"
           property="city">
      ${param.city}
    </c:set>

    Note

    -Option A is invalid because EL does not permit assignment.

    -Option C is invalid because the var attribute does not accept a runtime value, nor does it work with the property attribute.

  5. Which <body-content> element combinations in the TLD are valid for the following JSP snippet? (Choose all that apply.)

    (JSP v2.0 Appendix JSP.C specifically pgs 3-21 and 3-30)

    11. <my:tag1>
    12.   <my:tag2 a="47" />
    13.   <% a = 420; %>
    14.   <my:tag3>
    15.     value = ${a}
    16.   </my:tag3>
    17. </my:tag1>

    A.

    tag1 body-content is empty

    tag2 body-content is JSP

    tag3 body-content is scriptless

    B.

    tag1 body-content is JSP

    tag2 body-content is empty

    tag3 body-content is scriptless

    C.

    tag1 body-content is JSP

    tag2 body-content is JSP

    tag3 body-content is JSP

    D.

    tag1 body-content is scriptless

    tag2 body-content is JSP

    tag3 body-content is JSP

    E.

    tag1 body-content is JSP

    tag2 body-content is scriptless

    tag3 body-content is scriptless

    Note

    -Tag1 includes scripting code so it must have at least ‘JSP’ body-content. Tag2 is only shown as an empty tag, but it could also contain ‘JSP’ or ‘scriptless’ body-content. Tag3 contains no scripting code so it may have either ‘JSP’ or ‘scriptless’ body-content.

    -Option A is invalid because tag1 cannot be ‘empty’.

    -Option D is invalid because tag1 cannot be ‘scriptless’.

  6. Assuming the appropriate taglib directives, which are valid examples of custom tag usage? (Choose all that apply.)

    (JSP v2.0 section 7)

    A.

    <foo:bar />

    B.

    <my:tag></my:tag>

    C.

    <mytag value="x" />

    D.

    <c:out value="x" />

    E.

    <jsp:setProperty name="a" property="b" value="c" />

    Note

    -Option C is invalid because there is no prefix.

    -Option E is invalid because this is an example of a JSP standard action, not a custom tag.

  7. Given the following scriptlet code:

    (JSTL v1.1 pg 6-48)

    11. <select name='styleId'>
    12. <% BeerStyle[] styles = beerService.getStyles();
    13.    for ( int i=0; i < styles.length; i++ ) {
    14.      BeerStyle style = styles[i]; %>
    15.   <option value='<%= style.getObjectID() %>'>
    16.     <%= style.getTitle() %>
    17.   </option>
    18. <% } %>
    19. </select>

    Which JSTL code snippet produces the same result?

    A.

    <select name='styleId'>
            <c:for array='${beerService.styles}'>
              <option value='${item.objectID}'>${item.title}</option>
            </c:for>
          </select>

    B.

    <select name='styleId'>
      <c:forEach var='style' items='${beerService.styles}'>
        <option value='${style.objectID}'>${style.title}</option>
      </c:forEach>
    </select>

    C.

    <select name='styleId'>
      <c:for var='style' array='${beerService.styles}'>
        <option value='${style.objectID}'>${style.title}</option>
      </c:for>
    </select>

    D.

    <select name='styleId'>
      <c:forEach var='style' array='${beerService.styles}'>
        <option value='${style.objectID}'>${style.title}</option>
      </c:for>
    </select>

    Note

    -Option B is correct because it uses the proper JSTL tag/attribute names.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset