Coffee Cram: Chapter 8 Answers

  1. Given an HTML form that uses checkboxes to allow a user to select multiple values for a parameter called hobbies.

    (JSP v2.0 sections 2.2.3)

    Which EL expressions evaluate to the first value of the hobbies parameter? (Choose all that apply.)

    A.

    ${param.hobbies}

    B.

    ${paramValue.hobbies}

    C.

    ${paramValues.hobbies[0]}

    D.

    ${paramValues.hobbies[1]}

    E.

    ${paramValues[hobbies][0]}

    F.

    ${paramValues[hobbies][1]}

    Note

    -Option B is incorrect because there is no “paramValue” implicit variable.

    -Option D is incorrect, arrays are 0 indexed.

    -Options E and F have incorrect syntax.

  2. Given that a web application stores the webmaster email address in the servlet context initialization parameter called master-email.

    (JSP v2.0 sections 2.2.3 and 2.3.4)

    Which retrieves that value? (Choose all that apply.)

    A.

    <a href='mailto:${initParam.master-email}'>
        email me</a>

    B.

    <a href='mailto:${contextParam.master-email}'>
        email me</a>

    C.

    <a href='mailto:${initParam['master-email']}'>
        email me</a>

    D.

    <a href='mailto:${contextParam['master-email']}'>
        email me</a>

    Note

    -Option A is trying to subtract email from master

    -Option B, there is no contextParam implicit variable

    -Option D, there is no contextParam implicit variable

  3. Given the following Java class:

    1. package com.mycompany;
    2. public class MyFunctions {
    3.   public static String hello(String name) {
    4.     return "Hello "+name;
    5.   }
    6. }

    (JSP v2.0 section 2.6.3)

    This class represents the handler for a function that is part of a tag library. <%@ taglib uri=" http://mycompany.com.tags" prefix="comp" %> Which Tag Library Descriptor entry defines this custom function so that it can be used in an EL expression?

    A.

    <taglib>
      ...
      <tag>
        <name>Hello</name>
        <tag-class>com.mycompany.MyFunctions</tag-class>
        <body-content>JSP</body-content>
      </tag>
    </taglib>

    B.

    <taglib>
      ...
       <function>
         <name>Hello</name>
         <function-class>com.mycompany.MyFunctions</function-class>
         <function-signature>java.lang.String hello(java.lang.String)
              </function-signature>
       </function>
    </taglib>

    C.

    <web-app>
      ...
      <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.mycompany.MyFunctions</servlet-class>
      </servlet>
    </web-app>

    D.

    <taglib>
      ...
      <function>
        <name>Hello</name>
        <function-class>com.mycompany.MyFunctions</function-class>
        <function-signature>hello(java.lang.String)</function-signature>
      </function>
    </taglib>

    Note

    -Option B uses the correct syntax.

    -Option D is incorrect because the function signature is incomplete

  4. Given:

    1. package com.example;
    2. public class TheBean {
    3.   private int value;
    4.   public TheBean() { value = 42; }
    5.   public int getValue() { return value; }
    6.   public void setValue(int v) { value = v; }
    7. }

    (JSP v2.0 section 5.1)

    Assuming no instances of TheBean have been created yet, which JSP standard action statements create a new instance of this bean and store it in the request scope? (Choose all that apply.)

    A.

    <jsp:useBean name="myBean"
         type="com.example.TheBean" />

    B.

    <jsp:makeBean name="myBean"
         type="com.example.TheBean" />

    C.

    <jsp:useBean id="myBean"
         class="com.example.TheBean"
         scope="request" />

    D.

    <jsp:makeBean id="myBean"
         class="com.example.TheBean"
         scope="request" />

    Note

    -Option A is invalid because the type attribute is NOT used to create a new instance and the scope attribute must be specified (or defaults to page).

    -Option B is invalid for all of the above reasons plus jsp:makeBean is NOT a real tag.

    -Option D is invalid because jsp:makeBean is NOT a real tag.

  5. Given a Model 1 architecture in which a JSP page handles all of the controller functions, that JSP controller needs to dispatch the request to another JSP page.

    (JSP v2.0 section 5.5)

    Which standard action code will perform this dispatch?

    A.

    <jsp:forward page="view.jsp" />

    B.

    <jsp:forward file="view.jsp" />

    C.

    <jsp:dispatch page="view.jsp" />

    D.

    <jsp:dispatch file="view.jsp" />

    Note

    -Option A is correct (pg 1-110).

    -Option B is invalid because the forward action has no file attribute.

    -Options C and D are invalid because there is no dispatch action.

  6. Given:

    <% java.util.List list = new java.util.ArrayList();
       list.add("a");
       list.add("2");
       list.add("c");
       request.setAttribute("list", list);
       request.setAttribute("listIdx", "1");
    %>
    <%-- insert code here --%>

    (JSP v2.0 section 2.3.4)

    Which, inserted at line 18, are valid and evaluate to c ? (Choose all that apply.)

    A.

    ${list.2}

    B.

    ${list[2]}

    C.

    ${list.listIdx+1}

    D.

    ${list[listIdx+1]}

    E.

    ${list['listIdx' + 1]}

    F.

    ${list[list[listIdx]]}

    Note

    -Options A and C are incorrect because the dot operator cannot be used with a primitive.

    -Option E is incorrect because EL tries to coerce ‘listIdx’ to a Long which is invalid.

  7. Which statements about the . (dot) and [] EL operators are true? (Choose all that apply.)

    (JSP v2.0 pg. 1-69 )

    A.

    ${foo.bar} is equivalent to ${foo[bar]}

    B.

    ${foo.bar} is equivalent to ${foo["bar"]}

    C.

    ${foo["5"]} is valid syntax if foo is a Map

    D.

    ${header.User-Agent} is equivalent to ${header[User-Agent]}

    E.

    ${header.User-Agent} is equivalent to ${header["User-Agent"]}

    F.

    ${foo[5]} is valid syntax if foo is a List or an array

    Note

    -Option A is incorrect because it should be foo[“bar”].

    -Options D and E are incorrect because of the dash in User-Agent. Only header[“User-Agent”] will work.

  8. Given a JSP page with the line:

    ${101 % 10}

    (JSP v2.0 pg. 1-71)

    What will be displayed?

    A.

    1

    B.

    10

    C.

    1001

    D.

    101 % 10

    E.

    {101 % 10}

    Note

    -Option A is correct. The modulus operator returns the remainder of a division operation.

  9. Given:

    10. ${param.firstname}
    11. ${param.middlename}
    12. ${param.lastname}
    13. ${paramValues.lastname[0]}

    (JSP v2.0 pg 1-67 and pg 1-79)

    Which describes the output produced by this portion of a JSP page when passed the query string ?firstname=John&lastname=Doe?

    A.

    John Doe

    B.

    John Doe Doe

    C.

    John null Doe

    D.

    John null Doe Doe

    E.

    A null pointer exception will be thrown.

    Note

    -Option A is invalid because line 13 prints the user’s last name as well.

    -Options C and D are invalid because line 11 results in printing nothing rather than “null”.

  10. Which show valid usage of EL implicit variables? (Choose all that apply.)

    (JSP v2.0 pg. 1-66)

    A.

    ${cookies.foo}

    B.

    ${initParam.foo}

    C.

    ${pageContext.foo}

    D.

    ${requestScope.foo}

    E.

    ${header["User-Agent"]}

    F.

    ${requestDispatcher.foo}

    G.

    ${pageContext.request.requestURI}

    Note

    -Option A is incorrect because the variable is “cookie”.

    -Option C is incorrect because pageContext is NOT a Map and it doesn’t have a “foo” property.

    -Option F is incorrect because this is NOT an implicit object.

  11. Which are true about the <jsp:useBean> standard action? (Choose all that apply.)

    (JSP v2.0 pgs. 1-103 and pg. 1-104)

    A.

    The id attribute is optional.

    B.

    The scope attribute is required.

    C.

    The scope attribute is optional and defaults to request.

    D.

    Either the class or type attributes may be specified, but at least one.

    E.

    It is valid to include both the class attribute and the type attribute, even if their values are NOT the same.

    Note

    -Option A is incorrect because id is required.

    -Options B and C are incorrect because scope is optional and defaults to page.

  12. How would you include dynamic content in a JSP, similar to a server-side include (SSI)? (Choose all that apply.)

    (JSP v2.0 section 5.4)

    A.

    <%@ include file="/segments/footer.jspf" %>

    B.

    <jsp:forward page="/segments/footer.jspf" />

    C.

    <jsp:include page="/segments/footer.jspf" />

    D.

    RequestDispatcher dispatcher
      = request.getRequestDispatcher("/segments/footer.jspf");
    dispatcher.include(request,response);

    Note

    -Option A is incorrect because it uses an include directive, which is for static includes that happen at translation time.

    -Option D would be correct if it was a scriptlet: it functionally does the same thing as option C, but its syntax is only used by servlets.

  13. In an HTML page with a rich, graphical layout, which JSP standard action can be used to import an image file into the JSP page?

    (JSP v2.0 section 5.4)

    A.

    <jsp:image page="logo.png" />

    B.

    <jsp:image file="logo.png" />

    C.

    <jsp:include page="logo.png" />

    D.

    <jsp:include file="logo.png" />

    E.

    This CANNOT be done using a JSP standard action.

    Note

    -Options A and B are invalid because there is no image standard action.

    -Option C is invalid, not because the syntax of the include action is wrong, but because it does not make sense to import the binary data of the image file into the JSP content.

    -Option D is invalid because the include action does not take a file attribute.

    This is a tricky question because it is NOT possible to import the contents of any binary file into a JSP page, which generates an HTML response.

  14. Given:

    1. package com.example;
    2. public class MyFunctions {
    3.   public static String repeat(int x, String str) {
    4.      // method body
    5.   }
    6. }

    and given the JSP:

    1. <%@ taglib uri="/WEB-INF/myfuncts" prefix="my" %>
    2. <%-- insert code here --%>

    (JSP v2.0 section 2.6)

    Which, inserted at line 2 in the JSP, is a valid EL function invocation?

    A.

    ${repeat(2, "420")}

    B.

    ${repeat("2", "420")}

    C.

    ${my:repeat(2, "420")}

    D.

    ${my:repeat("2", "420")}

    E.

    A valid invocation CANNOT be determined.

    Note

    -Option E is correct. The necessary mapping information from the TLD is NOT known.

  15. Given:

    10. public class MyBean {
    11.   private java.util.Map params;
    12.   private java.util.List objects;
    13.   private String name;
    14.   public java.util.Map getParams() { return params; }
    15.   public String getName() { return name; }
    16.   public java.util.List getObjects() { return objects; }
    17. }

    (JSP v2.0 pg. 1-68)

    Which will cause errors (assume that an attribute named mybean can be found, and is of type MyBean)? (Choose all that apply.)

    A.

    ${mybean.name}

    B.

    ${mybean["name"]}

    C.

    ${mybean.objects.a}

    D.

    ${mybean["params"].a}

    E.

    ${mybean.params["a"]}

    F.

    ${mybean["objects"].a}

    Note

    -Options C and F will cause errors. “a” is NOT a List property, and since “objects” is NOT a Map, a lookup won’t be performed (as opposed to D and E).

  16. Given a JSP page:

    1. The user has sufficiently logged in or out:
    2.   ${param.loggedIn or param.loggedOut}.

    (JSP v2.0 pgs 1-66 and 1-73)

    If the request includes the query string “loggedOut=true", what will be this statement’s displayed value?

    A.

    The user has sufficiently logged in or out: false.

    B.

    The user has sufficiently logged in or out: true.

    C.

    The user has sufficiently logged in or out: ${param.loggedIn or param.loggedOut}.

    D.

    The user has sufficiently logged in or out: param.loggedIn or param.loggedOut.

    E.

    The user has sufficiently logged in or out: or true.

    Note

    -Option B is correct because the EL expression using “or” will return true if either loggedIn or loggedOut is true.

  17. Which about EL access operators are true? (Choose all that apply.)

    (JSP v2.0 pg. 1-69)

    A.

    Anywhere the . (dot) operator is used, the [] could be used instead.

    B.

    Anywhere the [] operator is used, the . (dot)could be used instead.

    C.

    If the . (dot) operator is used to access a bean property but the property doesn’t exist, then a runtime exception is thrown.

    D.

    There are some situations where the . (dot)operator must be used and other situations where the [] operator must be used.

    Note

    -Option B is incorrect because only the [] will work when accessing a) Lists and arrays, and b) Maps whose keys are not well-formed.

    -Option D is incorrect because the dot operator can always be converted to the [] operator.

  18. The following code fragment appears in a JSP page:

    <jsp:include page="/jspf/header.html"/>

    (JSP v2.0 section 5.4)

    The JSP page is part of a web application with the context root myapp.

    Given that the application’s top level directory is myapp, what is the path to the header.html file?

    A.

    /header.html

    B.

    /jspf/header.html

    C.

    /myapp/jspf/header.html

    D.

    /includes/jspf/header.html

    Note

    -The path /jspf/header.html when used as the value of the <jsp:include> action’s page attribute is relative to the web application, so a leading back slash (“/”) means “begin at the application’s top level.”

  19. An online jewelry retailer wishes to customize their online catalog for users who are logged in. They want to show specials for the user’s birthstone month. The company’s special offers are stored as a Map<String, Special[]> identified as specials in application scope and updated daily.

    (JSP v2.0 section 2.3.4)

    There is a bean stored as a session-scoped attribute named userInfo. Calling getBirthdate().getMonth() on this bean will return the user’s birthstone month.

    Which of the following code snippets could correctly retrieve the appropriate special offerings?

    A.

    ${applicationScope[userInfo.birthdate.month.specials]}

    B.

    ${applicationScope.specials[userInfo.birthdate.month]}

    C.

    ${applicationScope["specials"].userInfo.birthdate.month}

    D.

    ${applicationScope["userInfo.birthdate.month"].specials}

    Note

    -Option B correctly retrieves our Map<String, Special[]> from the application scope. It then attempts to get the month value from the user’s birthday and uses that as the key to search for a Special[] in the Map. Assuming a match is found in the Map, our Special[] is returned. This EL could be used in a c: forEach tag to iterate over the returned specials.

  20. A web based application for a major online movie rental retailer stores a List<Movie> as a session attribute to contain movies the user has requested. A random, embedded movie trailer from this list must display on the users’ main page every time the users’ main page is viewed.

    (JSP v2.0 section 2.6)

    Management thinks a similar feature will be needed in the near future on other pages that display lists of movies. Streaming video is accomplished with regular HTML, just like adding images to a page but with more complex tags.

    The development team needs a solution that is both flexible and maintainable. One possible solution is to create an EL function. The following statements are from a team meeting concerning EL functions as a solution to this problem. Which statements are true? (Choose all that apply.)

    A.

    EL functions can not solve this problem because they can not retrieve session attributes.

    B.

    The method implementing the EL function should not be declared static to give it access to session scope.

    C.

    The EL function can accept a parameter of java.util.List which will allow the needed movie list to be passed to it using EL.

    D.

    You might have to write HTML tags in the middle of Java code using an EL function, which is more difficult to maintain.

    Note

    -Option A: the movie list can be passed as a parameter to the function.

    -Option B: methods that implement EL functions must always be declared public and static.

    -Option C: a List may be passed to the function. Doing so provides a more flexible solution than one that requires your EL function to handle session scope as in options a and b.

    -Option D: the biggest reason not to choose an EL function as the total solution. The team chose to use a tag file as the solution but then also created an EL function that accepts a Collection and returns a random number based on the size of the collection.

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

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