How does session tracking differ between stateless and stateful protocols?

  • Stateful: Lightweight
  • Stateful: Stores client state
  • Stateless: No memory of previous requests
  • Stateless: Uses cookies
Stateless protocols, like HTTP, have no memory of previous requests, while stateful protocols, like HTTPS, store client state, allowing continuity across multiple requests.

To prevent character encoding issues when reading form data, the method __________ should be called before any getParameter methods.

  • encodeCharacters
  • handleEncoding
  • resolveCharacterIssues
  • setCharacterEncoding
To prevent character encoding issues when reading form data, the setCharacterEncoding method should be called before any getParameter methods. This ensures correct interpretation of characters in the form data.

The method __________ of the HttpServletRequest is used to read binary data from a form input.

  • getBinaryData()
  • getInputStream()
  • readBinaryData()
  • readFormData()
The getInputStream() method of the HttpServletRequest is used to read binary data from a form input.

When using RequestDispatcher's forward method, changes to the response _________ affect the final output.

  • always
  • may
  • will
  • will not
When using RequestDispatcher's forward method, changes to the response will not affect the final output, as the forward method transfers control without changing the response.

In which method of a servlet are most of the resources like threads, database connections, etc., released?

  • destroy()
  • doGet()
  • init()
  • service()
The destroy() method is called when a servlet is being removed from service, and it is used to release resources like threads and database connections.

To set multiple values for the same header, use the __________ method in HttpServletResponse.

  • addHeader()
  • appendHeader()
  • setHeader()
  • setMultipleValuesHeader()
The addHeader() method in HttpServletResponse is used to add multiple values for the same header, allowing for flexibility in handling headers with multiple values.

A servlet needs to read its own initialization parameters and also share a database connection pool. Which combination of objects should it use?

  • getInitParameter() and ServletContext
  • getServletConfig() and getServletContext()
  • getServletContext() and getInitParameter()
  • getServletContext() and getInitParameter()
To read its own initialization parameters, a servlet should use getInitParameter(). To share a database connection pool, it should use the ServletContext, which is accessible via getServletContext().

The __________ method is called to allow the servlet to respond to a request.

  • destroy()
  • doGet()
  • init()
  • service()
The service() method is called to allow the servlet to respond to a request. It handles the actual processing of the request.

Which Java method is used to add a cookie to the response object?

  • addCookie()
  • createCookie()
  • insertCookie()
  • setCookie()
The addCookie() method is used in Java to add a cookie to the response object and send it to the client.

A servlet is loaded at startup, processes several requests, and then is removed from the server. Identify the correct order of method invocations in its lifecycle.

  • destroy() -> service() -> init()
  • init() -> destroy() -> service()
  • init() -> service() -> destroy()
  • service() -> init() -> destroy()
The correct order of method invocations in the lifecycle of a servlet is init() (initialization) -> service() (processing requests) -> destroy() (removal from the server).