Coffee Cram: Chapter 10 Answers

  1. How can a Classic tag handler instruct the container to ignore the remainder of the JSP that invoked the tag? (Choose all that apply.)

    (JSP v2.0 pg 2-56)

    A.

    The doEndTag() method should return Tag.SKIP_BODY.

    B.

    The doEndTag() method should return Tag.SKIP_PAGE.

    C.

    The doStartTag() method should return Tag.SKIP_BODY.

    D.

    The doStartTag() method should return Tag.SKIP_PAGE.

    Note

    -Option A is invalid because this is not a valid return value for doEndTag().

    -Option C is invalid because it only causes the body of the tag to be skipped.

    -Option D is invalid because this is not a valid return value for doStartTag().

  2. Which directives and/or standard actions are applicable ONLY within tag files? (Choose all that apply.)

    A.

    tag

    B.

    page

    C.

    jsp:body

    D.

    jsp:doBody

    E.

    taglib

    Note

    (JSP v2.0 8.5 (pg 1-179)

    JSP v2.0 section 5.11

    JSP v2.0 section 5.12

    JSP v2.0 section 5.13)

    Note

    -Option A is valid (pg 1-179).

    -Option B is invalid because the page directive is never allowed in a tag file (pg 1-179).

    -Option C is invalid because the jsp:body action can appear in EITHER a tag file or JSP.

    -Option D is valid (pg 1-121).

    -Option E is invalid because the taglib directive can appear in EITHER a tag file or JSP.

  3. A medical website hides selective content from users who are not registered. In place of the hidden content, a message should display to encourage users to register. Given the Simple tag handler snippet:

    11. public void doTag() throws JspException, IOException {
    12.    String level =
    13.      (String) getJspContext().findAttribute("accountLevel");
    14.    if((level == null || "trial".equals(level))) {
    15.      String price = "?"; // TODO get context param
    16.      String message = "Content for paying members
                                 only.<br/>"+
    17.        "<a href="register.jsp">Sign up now for only
                           "+price+"!</a>";
    18.      getJspContext().getOut().write(message);
    19.    } else {
    20.      getJspBody().invoke(null);
    21.    }
    22. }

    At line 15, the price for registration should be retrieved from a context parameter named registrationFee, however there are no methods on JspContext for retrieving context parameters. What can solve this problem?

    A.

    Retrieve the value with pageContext.getServletContext().getInitParameter("registrationFee");

    B.

    Cast the JspContext to type PageContext so that you can use the methods of PageContext to retrieve the context parameter.

    C.

    Retrieve the value with getJspContext().findAttribute("registrationFee");

    D.

    Throw an exception to let the user know that the price could not be found.

    E.

    This is impossible with a Simple tag. A Classic tag must be used.

    Note

    -Option A the pageContext variable is only available to Classic tags.

    -Option B Correct. We never mentioned this trick and you won’t need to know it for the exam, but it might come in handy in the real world!

    -Option C Remember, we’re not looking for an attribute, we’re looking for a context parameter.

    - Option D Don’t give up so easily! With determination you can provide a good solution!

    -Option E is not impossible, just tricky.

  4. Which Simple tag mechanism will tell a JSP page to stop processing?

    (JSP v2.0 section 13.6.1)

    A.

    Return SKIP_PAGE from the doTag method.

    B.

    Return SKIP_PAGE from the doEndTag method.

    C.

    Throw a SkipPageException from the doTag method.

    D.

    Throw a SkipPageException from the doEndTag method.

    Note

    -Option A is invalid because the doTag method does not return a value.

    -Option B is invalid because a Simple tag does not have the doEndTag event method.

    -Option D is invalid because a Simple tag does not have the doEndTag event method.

  5. Which are true about the Classic tag model? (Choose all that apply.)

    (JSP v2.0 sections 13.1 and 13.2)

    A.

    The Tag interface can only be used to create empty tags.

    B.

    The SKIP_PAGE constant is a valid return value of the doEndTag method.

    C.

    The EVAL_BODY_BUFFERED constant is a valid return value of the doAfterBody method.

    D.

    The Tag interface only provides two values for the return value of the doStartTag method: SKIP_BODY and EVAL_BODY.

    E.

    There are three tag interfaces—Tag, IterationTag, and BodyTag—but only two built-in base classes: TagSupport, and BodyTagSupport.

    Note

    -Option A is invalid because the Tag interface can support tags with a body, but you can’t iterate or gain access to the body content.

    -Option C is invalid because doAfterBody can only return SKIP_BODY or EVAL_BODY_AGAIN.

    -Option D is invalid because doStartTag returns SKIP_BODY and EVAL_BODY_INCLUDE.

  6. Which must be true if you want to use dynamic attributes for a Simple tag handler? (Choose all that apply.)

    (JSP v2.0 section 13.3 pgs 2-74,75)

    A.

    Your Simple tag must NOT declare any static tag attributes.

    B.

    Your Simple tag must use the <dynamic-attributes> element in the TLD.

    C.

    Your Simple tag handler must implement the DynamicAttributes interface.

    D.

    Your Simple tag should extend the DynamicSimpleTagSupport class, which provides default support for dynamic attributes.

    E.

    Your Simple tag CANNOT be used with the jsp:attribute standard action, because this action works only with static attributes.

    Note

    -Option A is invalid because you can have both static and dynamic attributes in a Simple tag.

    -Option D is invalid because there is no such helper class in the built-in APIs.

    -Option E is invalid because you are allowed to use the jsp:attribute action with dynamic tags.

  7. Which is true about tag files? (Choose all that apply.)

    (JSP v2.0 section 8.4)

    A.

    A tag file may be placed in any subdirectory of WEB-INF.

    B.

    A tag file must have the file extension of .tag or .tagx.

    C.

    A TLD file must be used to map the symbolic tag name to the actual tag file.

    D.

    A tag file may NOT be placed in a JAR file in the WEB-INF/lib directory.

    Note

    -Option A is invalid because tag files must be placed under the WEB-INF/ tags directory.

    -Option B is correct (pg 1-176, 8.4.1).

    -Option C is invalid because tag files may be discovered by the container in several well-known locations. This container feature is optional.

  8. Given:

    (JSP v2.0 pg. 2-68)

    10. public class BufTag extends BodyTagSupport {
    11.   public int doStartTag() throws JspException {
    12.     // insert code here
    13.   }
    14. }

    Assume that the tag has been properly configured to allow body content.

    Which, if inserted at line 12, would cause the JSP code <mytags:mytag>BodyContent</mytags:mytag> to output BodyContent?

    A.

    return SKIP_BODY;

    B.

    return EVAL_BODY_INCLUDE;

    C.

    return EVAL_BODY_BUFFERED;

    D.

    return BODY_CONTENT;

    Note

    -Option A is invalid because it causes the body of the tag to be skipped.

    - Option C is invalid because it directs the body of the tag to a buffer which this tag does not process.

    -Option D is invalid because this is not a valid return code.

  9. Which about doAfterBody() is true? (Choose all that apply.)

    (JSP v2.0 pg. 1-152)

    A.

    doAfterBody() is only called on tags that extend TagSupport.

    B.

    doAfterBody() is only called on tags that extend IterationTagSupport.

    C.

    Assuming no exceptions occur, doAfterBody() is always called after doStartTag() for any tag that implements IterationTag.

    D.

    Assuming no exceptions occur, doAfterBody() is called after doStartTag() for any tag that implements IterationTag and returns SKIP_BODY from doStartTag().

    E.

    Assuming no exceptions occur, doAfterBody() is called after doStartTag() for any tag that implements IterationTag and returns EVAL_BODY_INCLUDE from doStartTag().

    Note

    -Option A is invalid because doAfterBody() can be called on any tag that implements the IteratorTag interface.

    -Option B is invalid because there is no such class.

    -Options C and D are invalid because doAfterBody() is only called when doStartTag() returns EVAL_BODY_INCLUDE.

  10. Given a JSP page:

    (JSP v2.0 TagSupport API pg 2-64)

    1. <%@ taglib prefix="my" uri="/WEB-INF/myTags.tld" %>
    2. <my:tag1>
    3.   <%-- JSP content --%>
    4. </my:tag1>

    The tag handler for my:tag1 is Tag1Handler and extends TagSupport.

    What happens when the instance of Tag1Handler calls the getParent method? (Choose all that apply.)

    A.

    A JspException is thrown.

    B.

    The null value is returned.

    C.

    A NullPointerException is thrown.

    D.

    An IllegalStateException is thrown.

    Note

    -Option B is the correct answer. The getParent method does not throw any exceptions.

  11. Which is true about the lifecycle of a Simple tag? (Choose all that apply.)

    (JSP v2.0 section 13.6 pgs 2-80/83)

    A.

    The release method is called after the doTag method.

    B.

    The setJspBody method is always called before the doTag method.

    C.

    The setParent and setJspContext methods are called immediately before the tag attributes are set.

    D.

    The JspFragment of the tag body is invoked by the Container before the tag handler’s doTag method is called. This value, a BodyContent object, is passed to the tag handler using the setJspBody method.

    Note

    -Option A is invalid because a Simple tag has no release method.

    -Option B is invalid because the setJspBody is not called if the Simple tag is an empty tag.

    -Option D is invalid because the fragment is invoked by the doTag implementation, NOT before the doTag is called.

  12. Given:

    (JSP v2.0 pg 2-27)

    10. public class ExampleTag extends TagSupport {
    11.   private String param;
    12.   public void setParam(String p) { param = p; }
    13.   public int doStartTag() throws JspException {
    14.     // insert code here
    15.     // more code here
    16.   }
    17. }

    Which, inserted at line 14, would be guaranteed to assign the value of the request-scoped attribute param to the local variable p? (Choose all that apply.)

    A.

    String p = findAttribute("param");

    B.

    String p = request.getAttribute("param");

    C.

    String p = pageContext.findAttribute("param");

    D.

    String p = getPageContext().findAttribute("param");

    E.

    String p = (String) pageContext.getRequest().getAttribute("param");

    Note

    -Option A is invalid because there is no such method.

    -Option B is invalid because there is no request instance variable.

    -Option C is invalid because an attribute in page scope would be found before checking request scope.

    -Option D is invalid because there is no getPageContext() method.

  13. Which are valid method calls on a PageContext object? (Choose all that apply.)

    (JSP v2.0 pg. 2-23)

    A.

    getAttributeNames()

    B.

    getAttribute("key")

    C.

    findAttribute("key")

    D.

    getSessionAttribute()

    E.

    findAttribute("key", PageContext.SESSION_SCOPE)

    F.

    getAttribute("key", PageContext.SESSION_SCOPE)

    Note

    -Options A and D are invalid because there are no methods with these names.

    -Option E is invalid because findAttribute() does not have a scope parameter.

  14. Which is the most efficient JspContext method to call to access an attribute that is known to be in application scope?

    (JSP v2.0 pg. 2-23)

    A.

    getPageContext()

    B.

    getAttribute(String)

    C.

    findAttribute(String)

    D.

    getAttribute(String, int)

    E.

    getAttributesScope("key")

    F.

    getAttributeNamesInScope(int)

    Note

    -Option A is invalid because there is no such method.

    -Option B is invalid because this method only looks in page scope.

    -Option C is invalid because this method would be less efficient than Option D because it first checks the other three scopes.

    -Option E is invalid because this method retrieves the attribute; it only tells you which scope it resides..

    -Option F is invalid because it would be only the first step in a process that would be much less efficient than Option D.

  15. What is the best strategy, when implementing a custom tag, for finding the value of an attribute whose scope is unknown?

    (JSP v2.0 pg. 2-23)

    A.

    Check all scopes with a single

    pageContext.getAttribute(String) call.

    B.

    Check all scopes with a single

    pageContext.findAttribute(String) call.

    C.

    Check each scope with calls to

    pageContext.getAttribute(String, int).

    D.

    Call pageContext.getRequest().getAttribute(String), then call pageContext.getSession().getAttribute(String), and so on.

    E.

    None of these will work.

    Note

    -Option A is invalid because this method only checks the page scope.

    -Options C and D are invalid because they are less efficient than simply calling findAttribute().

  16. Given a tag, simpleTag, whose handler is implemented using the Simple tag model and a tag, complexTag, whose handler is implemented using the Classic tag model. Both tags are declared to be non-empty and non-tag dependent in the TLD.

    (JSP v2.0 7.1.6 pg 1-156)

    Which JSP code snippets are valid uses of these tag? (Choose all that apply.)

    A.

    <my:simpleTag>
      <my:complexTag />
    </my:simpleTag>

    B.

    <my:simpleTag>
      <%= displayText %>
    </my:simpleTag>

    C.

    <my:simpleTag>
      <%@ include file="/WEB-INF/web/common/headerMenu.html" %>
    </my:simpleTag>

    D.

    <my:simpleTag>
      <my:complexTag>
        <% i++; %>
      </my:complexTag>
    </my:simpleTag>

    Note

    -Option A is correct; a Simple tag may include a Complex tag in the body as long as that tag contains no scripting code.

    -Option B is invalid because simple tags cannot have a body that includes a JSP expression tag.

    -Option C is correct because the include directive is processed before the body of the simpleTag is converted into a JspFragment; however, the included content must also be non-scripting (which is why this example includes an HTML segment).

    -Option D is not invalid because of the complexTag usage (as in Option A), but because the complexTag body has scripting code in it.

  17. Which are true about the Tag File model? (Choose all that apply.)

    (JSP v2.0 pg. 1-173)

    A.

    Each tag file must have a corresponding entry in a TLD file.

    B.

    All directives allowed in JSP pages are allowed in Tag Files.

    C.

    All directives allowed in Tag Files are allowed in JSP pages.

    D.

    The <jsp:doBody> standard action can only be used in Tag Files.

    E.

    The allowable file extensions for Tag Files are .tag and .tagx.

    F.

    For each attribute declared and specified in a Tag File, the container creates a page-scoped attribute with the same name.

    Note

    -Option A is invalid because tag files need only to be placed in the appropriate location in order to be used.

    -Option B is invalid because the page directive is not available in Tag Files.

    -Option C is invalid because the tag, attribute, and variable directives are not available in JSP pages.

  18. Which are valid in tag files? (Choose all that apply.)

    (JSP v2.0 pg. 1-174)

    A.

    <jsp:doBody />

    B.

    <jsp:invoke fragment="frag" />

    C.

    <%@ page import="java.util.Date" %>

    D.

    <%@ variable name-given="date"
        variable-class="java.util.Date" %>

    E.

    <%@ attribute name="name" value="blank"
        type="java.lang.String" %>

    Note

    -Option C is invalid because the page directive is not valid in tag files.

    -Option E is invalid because there is no value attribute defined for the attribute directive.

  19. Which returns the enclosing tag when called from within a tag handler class? (Choose all that apply.)

    (JSP v2.0 pg. 2-53)

    A. getParent()

     

    B. getAncestor()

     

    C. findAncestor()

     

    D. getEnclosingTag()

     

    Note

    -Option A is correct; it is the only one of the methods shown that exists.

  20. Given a web application structure:

    (JSP v2.0 pg. 1-176)

    /WEB-INF/tags/mytags/tag1.tag
    /WEB-INF/tags/tag2.tag
    /WEB-INF/tag3.tag
    /tag4.tag

    Which tags could be used by an appropriate taglib directive? (Choose all that apply.)

    A. tag1.tag

     

    B. tag2.tag

     

    C. tag3.tag

     

    D. tag4.tag

     

    Note

    -Options C and D are invalid because tag files must be placed under the /WEB-INF/tags directory or a subdirectory of /WEB-INF/tags.

  21. A web application includes many forms for users to fill out and submit. Nothing in the pages indicates that a field is required. Business decided that a red asterisk should be placed preceding the text labels of required fields but the project manager is contending that the background color of required fields be light blue and another department is demanding that the project’s application be consistent with their own, where the text of the labels be bold for required fields.

    (JSP v2.0 section 7)

    Considering the different perspectives on how required fields could be identified in pages, choose the most maintainable usage of a custom tag.

    A.

    <cust:requiredIcon/>First Name: <input type="text"
          name="firstName"/>

    B.

    <cust:textField label="First Name" required="true"/>

    C.

    <cust:requiredField color="red" symbol="*"
          label="First Name"/>

    D.

    <cust:required>
      First Name: <input type="text" name="firstName"/>
    </cust:required>

    Note

    -Option A would work if you knew that the required field would always be marked with a preceding symbol and the only potential change would be the identifier used. Even still, it would be just as simple to use an img tag and swap out a .gif icon in an images directory.

    -Option B is the most flexible solution. Your custom tag is given full control for constructing the label and text field and how they should be displayed.

    -Option C: specifying a color and symbol in the tag is an unsatisfactory solution, as a change to either of these values would require you to update the values of every tag in every JSP.

    -Option D: it would be possible to do things this way but your class implementing the tag would have to parse the body and manipulate it, creating a maintenance nightmare.

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

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