The ________ method of the HttpServletRequest interface is used to obtain the session object in servlets.

  • createSession()
  • getSession()
  • retrieveSession()
  • startSession()
The getSession() method of the HttpServletRequest interface is used to obtain the session object in servlets.

What is the purpose of the tag in a web.xml file?

  • Define servlet configuration
  • Map servlet to URL pattern
  • Specify servlet class
  • Specify servlet name
The tag in the web.xml file is used to define the configuration of a servlet. It provides information such as the servlet name, servlet class, and other configuration details, allowing the servlet container to understand how to manage and handle the servlet during the application's lifecycle.

The _________ method of RequestDispatcher is used for including content of another resource in the response.

  • execute()
  • forward()
  • include()
  • sendRedirect()
The include() method of RequestDispatcher is used for including content of another resource in the response.

Servlets can be used to handle ______, while JSP is used for presenting these to the users.

  • HTTP requests
  • business logic
  • database operations
  • presentation
Servlets are commonly used for handling business logic, database operations, etc., while JSP is primarily used for presenting content to users.

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.

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.

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.