How is ServletConfig initialized?

  • Automatically by the container
  • Using a configuration file
  • Using constructor
  • Using init() method
The ServletConfig is automatically initialized by the container. It provides configuration information to the servlet and can be obtained using the getServletConfig() method.

What is the difference between context parameters and initialization parameters in servlets?

  • Context parameters are set at the application level, while initialization parameters are specific to a servlet.
  • Context parameters are used for database connectivity, while initialization parameters are used for servlet configuration.
  • Initialization parameters are set at the application level, while context parameters are specific to a servlet.
  • Initialization parameters are used for database connectivity, while context parameters are used for servlet configuration.
Context parameters are set at the application level and are accessible to all servlets, while initialization parameters are specific to each servlet and are defined in the servlet's deployment descriptor (web.xml).

The response header 'Content-Disposition' with value 'attachment; filename="file.txt"' is set using the __________ method.

  • addHeader()
  • sendRedirect()
  • setContentType()
  • setHeader()
The setHeader() method is used to set response headers, including the 'Content-Disposition' for file downloads.

How can you securely send sensitive data from a client to a server in a web application?

  • Encode data in Base64
  • Send data in plain text
  • Use HTTP with custom encryption
  • Use HTTPS (SSL/TLS)
Sensitive data should be sent securely, and using HTTPS (SSL/TLS) ensures encrypted communication between the client and the server, providing a secure way to transmit sensitive information.

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.

In a servlet's lifecycle, which method is responsible for responding to client requests?

  • doGet()
  • doPost()
  • init()
  • service()
The service() method in a servlet's lifecycle is responsible for responding to client requests. It delegates the request to the appropriate method (e.g., doGet() or doPost()).