How can servlet initialization parameters be used effectively for database connectivity?

  • By embedding database connection details directly in the servlet code.
  • By relying on default database configurations provided by the servlet container.
  • By storing database connection details as initialization parameters and retrieving them in the servlet's init() method.
  • By using context parameters instead of initialization parameters for database connectivity.
Servlet initialization parameters can be used effectively for database connectivity by storing database connection details in the web.xml file and retrieving them in the servlet's init() method for establishing connections.

A cookie's security can be enhanced by setting the _________ flag, which prevents its access via JavaScript.

  • HttpOnly
  • Max-Age
  • Path
  • Secure
The HttpOnly flag enhances a cookie's security by preventing its access via JavaScript.

When designing a servlet that handles sensitive data, which header should be set to secure the response?

  • Access-Control-Allow-Origin, setHeader()
  • Strict-Transport-Security, setHeader()
  • X-Content-Type-Options, setHeader()
  • X-Frame-Options, setHeader()
To secure the response when handling sensitive data, the Strict-Transport-Security header should be set using the setHeader() method in the HttpServletResponse.

The scope of ServletContext is:

  • Application
  • Page
  • Request
  • Session
The ServletContext has the scope of the entire application (web context). It is used to share information across servlets in the same web application and is often used for initialization and configuration.

What is the difference in the URL seen by the client when using forward() vs sendRedirect() in servlets?

  • The client sees the new URL in both cases
  • The client sees the new URL with forward() and the original URL with sendRedirect()
  • The client sees the original URL in both cases
  • The client sees the original URL with forward() and the new URL with sendRedirect()
The difference lies in what the client sees: with forward(), the client sees the original URL, while with sendRedirect(), the client sees the new URL.

How would a servlet handle different initialization parameters for different deployment environments?

  • Create separate servlets for each environment
  • Leverage servlet annotations for environment-specific settings
  • Use a single set of parameters for all environments
  • Use conditional logic within the servlet
Servlets can handle different initialization parameters for different deployment environments by using conditional logic within the servlet. This allows the servlet to adapt its behavior based on the specific configuration for each environment.

What is the role of the Secure flag in a cookie?

  • Enables third-party access to the cookie
  • Ensures the cookie is encrypted
  • Indicates the cookie is safe for cross-site requests
  • Restricts the cookie to HTTPS connections
The Secure flag in a cookie indicates that the cookie should only be sent over secure, encrypted connections (HTTPS). This enhances the security of the cookie by preventing it from being transmitted over unsecured HTTP connections.

HTTP persistent connections are managed using the _________ header in servlet responses.

  • Connection
  • Keep-Alive
  • Persistent
  • Session
HTTP persistent connections are managed using the Keep-Alive header in servlet responses.

The __________ method is generally used for fetching data where the request does not affect server state.

  • DELETE
  • GET
  • POST
  • PUT
The GET method is generally used for fetching data where the request does not affect server state. It is considered idempotent.

To access all initialization parameters, the method ________ can be used, which returns an _________.

  • getInitParameterNames(), Enumeration
  • getInitParameters(), ArrayList
  • getParameters(), Array
  • getServletContext(), Enumeration
The correct method is getInitParameterNames(), which returns an Enumeration containing the names of the servlet's initialization parameters.