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().

How does an HTTP servlet handle multipart/form-data requests?

  • It accesses the data using request.getParts() method.
  • It employs the request.getInputStream() method.
  • It uses the HttpServletRequest.getParameter() method.
  • It utilizes the MultipartRequest class.
Handling multipart/form-data requests in an HTTP servlet involves using the request.getParts() method to access the various parts of the request, allowing extraction of file uploads and other data.

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

If a servlet needs to log application-wide events, which object would be the best choice for retrieving the log file’s path?

  • HttpServletRequest
  • HttpServletResponse
  • ServletConfig
  • ServletContext
The ServletContext provides a way to retrieve information that is shared among servlets, making it suitable for obtaining the log file's path for application-wide event logging.

What technology is typically used for writing presentation logic in web applications?

  • Applet
  • JSP
  • JavaBean
  • Servlet
JavaServer Pages (JSP) is typically used for writing presentation logic in web applications, allowing Java code to be embedded into HTML for dynamic content generation.

How does cookie-based session tracking work in servlets?

  • Cookies are not used for session tracking in servlets.
  • Cookies are used only for authentication in servlets.
  • Cookies are used to store session information on the client side.
  • Cookies are used to store session information on the server side.
Cookie-based session tracking in servlets involves storing session information on the client side using cookies. This allows the server to recognize and associate subsequent requests with the same session.

How does the HttpSession interface facilitate session tracking?

  • It allows direct manipulation of the HTTP response.
  • It enables the creation and management of sessions.
  • It is responsible for URL rewriting in session tracking.
  • It provides methods to manipulate cookies.
The HttpSession interface in servlets facilitates session tracking by enabling the creation and management of sessions. It provides methods to store, retrieve, and manage session attributes for a particular client.

Which method is used to retrieve a servlet's initialization parameter in the servlet code?

  • getInitParam()
  • getServletConfig().getInitParameter()
  • getServletContext().getInitParam()
  • request.getParameter()
The method getServletConfig().getInitParameter() is used to retrieve a servlet's initialization parameter in the servlet code.