When a form is submitted with the enctype as 'multipart/form-data', how should the form data be handled in a servlet?

  • This is not supported in servlets
  • Use a library like Apache FileUpload
  • Use the getInputStream()
  • Use the getParameter()
When a form is submitted with the enctype as 'multipart/form-data', the form data should be handled using a library like Apache FileUpload for efficient processing of multipart data in servlets.

How does the servlet container pass initialization parameters to the servlet?

  • Through XML configuration
  • Through environment variables
  • Through method parameters
  • Through web.xml
The servlet container passes initialization parameters to the servlet through the web.xml configuration file.

If a servlet needs to perform some action repeatedly every time a request is received, which method is most appropriate for placing such code?

  • doGet()
  • doPost()
  • init()
  • service()
The service() method is the most appropriate for placing code that needs to be executed repeatedly every time a request is received. This method is invoked for each incoming request and can handle various HTTP methods like GET, POST, etc.

What method would you use to set the expiration date of a servlet response as a specific date and time?

  • response.setDateHeader("Expires", System.currentTimeMillis() + 86400000);
  • response.setExpirationDate("2024-12-31T23:59:59");
  • response.setExpirationTime("2024-12-31T23:59:59");
  • response.setExpires("2024-12-31T23:59:59");
The response.setDateHeader("Expires", System.currentTimeMillis() + 86400000); method is used to set the expiration date of a servlet response to a specific date and time.

What is the role of servlet-mapping in the servlet's configuration?

  • It configures the servlet's initialization parameters.
  • It defines the servlet's behavior in handling HTTP requests.
  • It determines the servlet's load-on-startup configuration.
  • It specifies the URL pattern to associate with a servlet.
Servlet-mapping in the web.xml file associates a URL pattern with a servlet, specifying which requests should be directed to that servlet for processing. It helps in defining the servlet's mapping to incoming URLs.

During the servlet lifecycle, which method is called only once and is used for initialization purposes?

  • destroy()
  • doGet()
  • init()
  • service()
The init() method is called only once when a servlet is first loaded into memory and is used to perform any necessary initialization tasks.

Can a servlet access the ServletConfig of another servlet?

  • It depends on the web container being used.
  • No
  • Only if both servlets belong to the same package.
  • Yes
No, a servlet cannot directly access the ServletConfig of another servlet. Each servlet has its own ServletConfig associated with it.

If an HTTP servlet receives a request with an invalid session token, what should it do next?

  • Generate a new session token and proceed
  • Ignore the request
  • Redirect to the login page
  • Send an HTTP 401 Unauthorized response
In the case of an invalid session token, it's a security best practice to send an HTTP 401 Unauthorized response to prompt reauthentication.

Which method of the RequestDispatcher interface is used to forward a request from a servlet to another resource?

  • dispatch()
  • forward()
  • forwardRequest()
  • redirect()
The forward() method of the RequestDispatcher interface is used to forward a request from a servlet to another resource within the same server.

What is the key difference in the way servlets and JSPs are compiled?

  • Both servlets and JSPs are compiled just-in-time (JIT)
  • Both servlets and JSPs are interpreted
  • JSPs are precompiled
  • Servlets are precompiled
Servlets are precompiled into bytecode during the build process, while JSPs are typically compiled into servlets at runtime. This compilation difference contributes to the performance variations between servlets and JSPs.