A servlet needs to handle a file upload from a web form. Which HTTP method and content type should be used?

  • DELETE with text/plain
  • GET with application/x-www-form-urlencoded
  • POST with multipart/form-data
  • PUT with application/json
For handling file uploads, the appropriate combination is to use the POST method along with the multipart/form-data content type.

ServletContext allows servlets to __________ resources and information.

  • access
  • initialize
  • restrict
  • share
ServletContext allows servlets to share resources and information.

How does the servlet container handle thread safety in the case of servlets?

  • By creating a new thread for each request.
  • By making the service() method synchronized.
  • By using a single thread for all requests.
  • Thread safety is the responsibility of the developer.
The servlet container handles thread safety by making the service() method synchronized to ensure that only one thread executes it at a time, preventing race conditions in shared resources.

What is the impact of calling request.getParameter() after request.getInputStream() or request.getReader() in a servlet?

  • It will result in a compilation error.
  • It will retrieve the parameter value successfully.
  • It will return null.
  • It will throw a runtime exception.
Calling request.getParameter() after request.getInputStream() or request.getReader() will return null because the input stream or reader can be consumed only once, and they might have already been read.

A servlet needs to load database settings at startup. How should these settings be passed to the servlet?

  • Hardcode the settings in the servlet code
  • Store settings in a local properties file
  • Use ServletContext parameters
  • Use request parameters
The recommended approach is to use ServletContext parameters to pass settings at servlet startup, as they can be accessed by all servlets within the same web application. Hardcoding or using request parameters may not be suitable for initialization settings.

Which HTTP method is idempotent: GET or POST?

  • DELETE
  • GET
  • POST
  • PUT
The GET method is idempotent, meaning multiple identical requests have the same effect as a single request. This is because GET requests do not change the state on the server.

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.

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.