Servlets and JSP

Pēdējais mainījis superadmin 2018-01-12 20:23

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

Jauns jautājums:

Prasmes

  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

  • 1001

    Read the following code snippet. {code:xml}
    <taglib>
        <taglib-uri>
               http://www.yourcompany.com/yourTagLibrary
        </taglib-uri>
        <taglib-location>
           /WEB-INF/yourTagLibrary.tld
        </taglib-location>
    </taglib>
    {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

  • 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>

  • 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}

  • 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>

  • 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

  • 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>

  • 3003

    Assuming that the servlet method for handling HTTP GET requests is <tt>doGet(HttpServletRequest request, HttpServletResponse response)</tt>, 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>

  • 3004

    Assuming that the servlet method for handling HTTP GET requests is <tt>doGet(HttpServletRequest request, HttpServletResponse response)</tt>, which two statements are true regarding the following code snippet? {code}
    Enumeration enum = request.getParameterNames();
    {code}

    Izvēlēties 2 no N
    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}

  • 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.

  • 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

  • 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

  • 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>

  • 4003

    Which two of the following are valid attributes of the JSP page directive?

    Izvēlēties 2 no N
    1. <tt>extends="package.class"</tt>

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

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

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

  • 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>

  • 4005

    Which one of the following is not an attribute of the <tt>jsp:useBean</tt> tag?

    1. <tt>scope</tt>

    2. <tt>name</tt>

    3. <tt>type</tt>

    4. <tt>class</tt>

  • 4006

    Which two of the following pairs are equivalents?

    Izvēlēties 2 no N
    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}

  • 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.

  • 4008

    Read the following listing. {code:xml}

    1.  <html>
      2.    <body>
      3.
      4.      &lt;% x = x - 15; %>
      5.      New customers: &lt;%= x %>
      6.    </body>
      7.  </html>
      {code} What should be inserted on line 3 to allow successful compilation and produce an output of <b>New customers: 20</b>?
    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}

  • 4009

    Read the following code snippet. {code}
    &lt;%= 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

  • 5001

    Which two of the following methods are used to access object attributes within the session scope?

    Izvēlēties 2 no N
    1. <tt>getNames</tt>

    2. <tt>getSessionAttributes</tt>

    3. <tt>getAttribute</tt>

    4. <tt>getAttributeNames</tt>

  • 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>

  • 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>

  • 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>

  • 5005

    Which two of the following methods are used to access resources within the context scope?

    Izvēlēties 2 no N
    1. <tt>getSession</tt>

    2. <tt>getNamedDispatcher</tt>

    3. <tt>getDispatcher</tt>

    4. <tt>getRequestDispatcher</tt>

  • 5006

    Which two of the following methods are used to access values and resources and to set object attributes within the session scope?

    Izvēlēties 2 no N
    1. <tt>setAttribute</tt>

    2. <tt>removeAttributeName</tt>

    3. <tt>deleteAttribute</tt>

    4. <tt>removeAttribute</tt>

6. Servlet Deployment

  • 6001

    Which two of the following statements are true regarding declaring a servlet instance in a deployment descriptor?

    Izvēlēties 2 no N
    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>.

  • 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.

Citi jautājumi

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