In a servlet, how can you retrieve parameters sent via the POST method?

  • request.getParameter()
  • request.getParameters()
  • request.getPostParameters()
  • request.retrievePostParams()
Parameters sent via the POST method in a servlet can be retrieved using the request.getParameter() method.

The __________ method is used to read initialization parameters from ServletConfig.

  • getConfig()
  • getInitParameter()
  • getParameters()
  • initParams()
The getInitParameter() method is used to read initialization parameters from ServletConfig.

A servlet receives form data with both text and file inputs. Identify the correct approach to parse this data.

  • Convert the data to JSON and parse it
  • Parse the data manually using String methods
  • Use a library like Apache Commons FileUpload
  • Use the built-in Java Scanner class
The correct approach is to use a library like Apache Commons FileUpload, which is designed to handle form data with both text and file inputs efficiently and reliably.

What is the significance of the Last-Modified header in HTTP servlet responses?

  • It controls the cache behavior for the servlet response.
  • It indicates the last modification time of the servlet.
  • It signals the client to request the servlet again.
  • It specifies the expiration time of the servlet.
The Last-Modified header informs the client about the last modification time of the servlet, allowing the client to cache the response and avoid unnecessary requests if the content hasn't changed.

If a client application needs to request a large amount of data without affecting the server's state, which method should it use and why?

  • DELETE, because it is a safe method for retrieving data.
  • GET, because it is idempotent and does not modify the server's state.
  • POST, because it supports larger data payloads than GET.
  • PUT, because it is specifically designed for requesting large data sets.
The GET method is idempotent and does not modify the server's state, making it suitable for requesting large amounts of data without side effects. POST, although supporting larger payloads, is not intended for safe, idempotent operations.

When a servlet encounters an error during initialization, which method gets invoked next?

  • destroy()
  • doError()
  • initError()
  • service()
If a servlet encounters an error during initialization, the initError() method is invoked next to handle the initialization error.

Which one directly interacts with the Java server pages (JSP) container?

  • CSS
  • HTML
  • JSP
  • Servlets
Servlets directly interact with the JavaServer Pages (JSP) container.

A ________ is a unique identifier generated by the server and sent to the client to maintain a session.

  • Cookie
  • Session ID
  • Token
  • URL Rewriting
The term for a unique identifier generated by the server and sent to the client to maintain a session is a Cookie.

What is a significant difference in how session data is handled between RequestDispatcher's forward and HttpServletResponse's sendRedirect?

  • Session data is duplicated
  • Session data is lost
  • Session data is modified
  • Session data is shared
When using HttpServletResponse's sendRedirect, the session data is typically lost because it involves a new request. In contrast, RequestDispatcher's forward method allows the sharing of session data, maintaining it throughout the forwarding process, providing a more seamless user experience with consistent session information.

The _________ attribute specifies the subset of URLs to which a cookie will be sent.

  • Domain
  • Expires
  • Path
  • Secure
The Domain attribute specifies the subset of URLs to which a cookie will be sent.