Servlets and JSP

Last modified by superadmin on 2018-01-12 20:23

ItemSet: Servlets and JSP

This test covers basic things that are defined in Servlet 2.4 (and JSP 2.0) specifications and used by Java Web applications

New item:

Skills

  1. Tag Libraries

    Understand how to declare and use tag libraries.

    1. Servlet Lifecycle

    Understand servlet creation, initialization and destruction

    1. Issues of Servlet Programming

    Serving requests, receiving HTTP parameters, handling responses, logging, etc.

    1. Issues of JSP Programming

    Directives, expressions, scriptlets; "jsp:useBean" and related constructs.

    1. Session State, Scopes

    Various scopes, sessions and HTTP mechanisms related to <tt>HttpSession</tt>, synchronization issues.

    1. Servlet Deployment

    Understand how servlet container interacts with servlets and JSPs depending on the descriptor <tt>web.xml</tt>

1. Tag Libraries

  1. ServletSpec_1001: Read the following code snippet. {code:xml} http://www.yourcompany.com/yourTagLibrary /WEB-INF/yourTagLibrary.tld {code} What statement applies to this code snippet?

    1. The tablib element of the deployment descriptor is required if you use a taglib directive in a JSP page that references a TLD by name (indirectly) rather than by location (directly).

    2. This tag goes into the WEB-INF/myApplication/web.xml file.

    3. This definition is incorrect because the taglib-uri should have been uri.

    4. This definition is incorrect because the taglib should have been tag.

2. Servlet Lifecycle

  1. ServletSpec_2001: How would you retrieve servlet-initialization parameters? (By "paramName" we denote the name of a parameter; by "paramIndex" - its number in the order of declarations).

    1. <tt>String value = getServletConfig().getInitParameter("<i>paramName</i>");</tt>

    2. <tt>String value = getServletConfig().getInitParameter(<i>paramIndex</i>);</tt>

    3. <tt>String value = getServletConfig().getParameter("<i>paramName</i>");</tt>

    4. <tt>String value = getServletConfig().getParameter(<i>paramIndex</i>);</tt>

  2. ServletSpec_2002: How do you declare initialization parameters for the servlet in the deployment descriptor?

    1. {code:xml}<web-app>
        <servlet>
          <servletname>servletName</servletname>
          <servletclass>com.myCo.MyServlet</servletclass>
          <initparam>
            <paramname>color</paramname>
            <paramvalue>blue</paramvalue>
          </initparam>
        </servlet>
      </web-app>{code}

    2. {code:xml}
      <web-app>
        <servlet>
          <servlet-name>servletName</servlet-name>
          <servlet-class>com.myCo.MyServlet</servlet-class>
          <init-param>
            <name>color</name>
            <value>blue</value>
          </init-param>
        </servlet>
      </web-app>
      {code}

    3. {code:xml}
      <web-app>
        <servlet>
          <name>servletName</name>
          <class>com.myCo.MyServlet</class>
          <init-param>
            <param-name>color</param-name>
            <param-value>blue</param-value>
          </init-param>
        </servlet>
      </web-app>
      {code}

    4. {code:xml}
      <web-app>
        <servlet>
          <servlet-name>servletName</servlet-name>
          <servlet-class>com.myCo.MyServlet</servlet-class>
          <init-param>
            <param-name>color</param-name>
            <param-value>blue</param-value>
          </init-param>
        </servlet>
      </web-app>
      {code}

  3. ServletSpec_2003: What method is called by the servlet container just before this servlet is put into service?

    1. <tt>public void init() throws IOException { code }</tt>

    2. <tt>public void start() throws IOException { code }</tt>

    3. <tt>public void service() throws ServletException { code }</tt>

    4. <tt>public void init() throws ServletException { code }</tt>

  4. ServletSpec_2004: What method is called by the servlet container just after the servlet is removed from service?

    1. <tt>public void destroy() {  code... }</tt>

    2. <tt>public void destroy() throws ServletException {  code... }</tt>

    3. <tt>public void finalize() throws ServletException {  code... }</tt>

    4. <tt>public void finalize() {  code... }</tt>

3. Issues of Servlet Programming

  1. ServletSpec_3002: How would you add an entry to the log file from a servlet assuming that e is an initialized Exception object?

    1. <tt>getServletConfig().log("An exception occurred", e);</tt>

    2. <tt>getServletConfig().logger(e);</tt>

    3. <tt>getServletContext().log(e);</tt>

    4. <tt>getServletContext().log("An exception occurred", e);</tt>

  2. ServletSpec_3003: Assuming that the servlet method for handling HTTP GET requests is doGet(HttpServletRequest request, HttpServletResponse response), how do you get a request parameter in that servlet?

    1. <tt>String value = request.getInitParameter("name");</tt>

    2. <tt>String value = response.getParameter("name");</tt>

    3. <tt>String value = request.getParameter("name");</tt>

    4. <tt>String value = request.getInitParameter(10);</tt>

  3. ServletSpec_3004: Assuming that the servlet method for handling HTTP GET requests is doGet(HttpServletRequest request, HttpServletResponse response), which two statements are true regarding the following code snippet? {code} Enumeration enum = request.getParameterNames(); {code} ~~Select 2 correct answers!~~

    1. This is how you retrieve the names of all request parameters.

    2. This is how you would then get the name of the next request parameter: {code}
      name = (String)enum.nextElement();
      {code}

    3. The code snippet in the question will not work. The return type is a <tt>String</tt> array, not an <tt>Enumeration</tt> object.

    4. This would have worked had the code been: {code}
      String[] names = request.getParameterNames();
      {code}

  4. ServletSpec_3005: Which one of the following is the most likely trigger to cause a browser to use the HTTP HEAD method?

    1. Client sends updated information to server.

    2. Server sends HTTP HEAD to client.

    3. Server requests HTTP HEAD of client.

    4. Client checks resource modification date/time.

  5. ServletSpec_3006: What interface and method name should be used to acquire a binary stream for the response?

    1. <tt>ServletResponse.getOutputStream</tt>

    2. <tt>ServletResponse.getStream</tt>

    3. <tt>ServletResponse.getWriterStream</tt>

    4. <tt>ServletResponse.getWriter</tt>

4. Issues of JSP Programming

  1. ServletSpec_4001: How do you perform a server-side include whose content gets translated within the JSP page?

    1. <tt>include</tt> action

    2. <tt>include</tt> directive

    3. <tt>include</tt> scriptlet

    4. <tt>include</tt> expression

  2. ServletSpec_4002: How would you include dynamic content in a JSP page that doesn't get translated within the context of the current page?

    1. <tt>&lt;%@ page import="footer.html" %></tt>

    2. <tt>include</tt> directive

    3. <tt>&lt;%@include file="footer.html" %></tt>

    4. <tt>jsp:include</tt>

  3. ServletSpec_4003: Which two of the following are valid attributes of the JSP page directive? ~~Select 2 correct answers!~~

    1. <tt>extends="package.class"</tt>

    2. <tt>errorPage="true"</tt>

    3. <tt>buffer="false"</tt>

    4. <tt>import="package.class"</tt>

  4. ServletSpec_4004: What is an example of an expression?

    1. <tt>&lt;%= count %></tt>

    2. <tt>&lt;% String count=4; %></tt>

    3. <tt>&lt;% out.print("hello"); %></tt>

    4. <tt>&lt;%! String count=4; %></tt>

  5. ServletSpec_4005: Which one of the following is not an attribute of the jsp:useBean tag?

    1. <tt>scope</tt>

    2. <tt>name</tt>

    3. <tt>type</tt>

    4. <tt>class</tt>

  6. ServletSpec_4006: Which two of the following pairs are equivalents? ~~Select 2 correct answers!~~

    1. {code}
      <% yourCode %>
      {code} and {code}
      <jsp:scriptlet>yourCode</jsp:scriptlet>
      {code}

    2. {code}
      ServletContext.useBean("beanName",id,scope, class);
      {code} and {code}
      <jsp:useBean id="customer"
        scope="page" class="session.Customer" />
      {code}

    3. {code}
      <%@ taglib uri="URIForLibrary" prefix="tagPrefix" %>
      {code} and {code}
      <jsp:taglib uri="URIForLibrary" prefix="tagPrefix" />
      {code}

    4. {code}
      <%@ page att="val" %>
      {code} and {code}
      <jsp:directive.page att="val"/>
      {code}

  7. ServletSpec_4007: How can you set several attributes of a Bean at the same time?

    1. You can't. They must be done individually.

    2. You do this with the deployment descriptor.

    3. You use the <tt>jsp:setProperty</tt> action with the property attribute set to the wildcard <tt>\*</tt>.

    4. You use the <tt>jsp:useBean</tt> and list the attributes in this tag together.

  8. ServletSpec_4008: Read the following listing. {code:xml}
    1. 2. 3. 4. <% x = x - 15; %> 5. New customers: <%= x %> 6. 7. {code} What should be inserted on line 3 to allow successful compilation and produce an output of New customers: 20?

      1. {code}&lt;%! int x = 35 %>{code}

      2. {code}&lt;%@ int x = 35; %>{code}

      3. {code}&lt;% int x = 35 ; %>{code}

      4. {code}int x = 35;{code}

    2. ServletSpec_4009: Read the following code snippet. {code} <%= myBean.getColor() %> {code} What option will produce the same result?

      1. <tt>&lt;jsp:getProperty name=myBean property=color /></tt>

      2. <tt>&lt;jsp:getProperty "myBean.getColor" /></tt>

      3. <tt>&lt;jsp:getProperty bean1.getColor/></tt>

      4. <tt>&lt;jsp:getProperty name="myBean" property="color" /></tt>

5. Session State, Scopes

  1. ServletSpec_5001: Which two of the following methods are used to access object attributes within the session scope? ~~Select 2 correct answers!~~

    1. <tt>getNames</tt>

    2. <tt>getSessionAttributes</tt>

    3. <tt>getAttribute</tt>

    4. <tt>getAttributeNames</tt>

  2. ServletSpec_5002: What is the interface used to access values and resources and to set object attributes within the session scope?

    1. <tt>HttpSession</tt>

    2. <tt>RequestSession</tt>

    3. <tt>HttpRequestSession</tt>

    4. <tt>Session</tt>

  3. ServletSpec_5003: What method accesses attributes within the request scope?

    1. <tt>getAttributes</tt>

    2. <tt>getNames</tt>

    3. <tt>getAttributeNames</tt>

    4. <tt>getNamedAttribute</tt>

  4. ServletSpec_5004: How could you reference a session in a servlet?

    1. <tt>Session mySession = Context.getSession();</tt>

    2. <tt>Session mySession = pageContext.getHttpSession();</tt>

    3. <tt>HttpSession mySession = pageContext.getHttpSession();</tt>

    4. <tt>HttpSession mySession = pageContext.getSession();</tt>

  5. ServletSpec_5005: Which two of the following methods are used to access resources within the context scope? ~~Select 2 correct answers!~~

    1. <tt>getSession</tt>

    2. <tt>getNamedDispatcher</tt>

    3. <tt>getDispatcher</tt>

    4. <tt>getRequestDispatcher</tt>

  6. ServletSpec_5006: Which two of the following methods are used to access values and resources and to set object attributes within the session scope? ~~Select 2 correct answers!~~

    1. <tt>setAttribute</tt>

    2. <tt>removeAttributeName</tt>

    3. <tt>deleteAttribute</tt>

    4. <tt>removeAttribute</tt>

6. Servlet Deployment

  1. ServletSpec_6001: Which two of the following statements are true regarding declaring a servlet instance in a deployment descriptor? ~~Select 2 correct answers!~~

    1. The servlet element specifies the fully qualified class name of the servlet.

    2. The tags are nested within <tt>&lt;web-app> &lt;/web-app></tt> tags.

    3. The tags do not define parameters.

    4. The tags are <tt>&lt;servlet-instance>&lt;/servlet-instance></tt>.

  2. ServletSpec_6002: How many servlet mappings in a deployment descriptor may be defined for one Web app?

    1. Unlimited.

    2. Servlet mappings don't go into the deployment descriptor.

    3. Zero.

    4. One.

Unclassified Questions

Tags:
Created by Kalvis Apsītis on 2007-12-31 21:17
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation