What differentiates ServletContext and ServletConfig in terms of initialization parameters?

  • ServletConfig can be used for application-wide parameters, while ServletContext is specific to a servlet.
  • ServletConfig is used for application-wide parameters.
  • ServletContext can be used for application-wide parameters, while ServletConfig is specific to a servlet.
  • ServletContext is used for servlet-specific parameters.
The ServletContext is intended for parameters that are shared across the entire application, whereas ServletConfig is specific to a particular servlet.

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.

If a servlet generates dynamic content and you want to ensure it's not cached, which combination of response headers should be set?

  • Cache-Control: no-store, Pragma: no-cache
  • ETag, Cache-Control: no-cache
  • Expires: 0, Cache-Control: must-revalidate
  • Last-Modified, Cache-Control: private
To ensure dynamic content is not cached, the combination of Cache-Control: no-store and Pragma: no-cache headers should be set in the HttpServletResponse.

Which allows embedding Java code directly into the HTML code?

  • Applet
  • JSP
  • JavaBean
  • Servlet
JavaServer Pages (JSP) allows the embedding of Java code directly into HTML code, facilitating the creation of dynamic web pages with server-side logic.

In the lifecycle of JSP, after the translation phase, it undergoes the ______ process.

  • compilation
  • destruction
  • execution
  • initialization
After the translation phase, JSP undergoes the execution process, where the generated servlet is executed to produce dynamic content.

How can a servlet handle form data containing non-ASCII characters?

  • Convert the data to ASCII
  • Set the character encoding using setCharacterEncoding()
  • This is not possible
  • Use the getEncoding()
To handle form data containing non-ASCII characters, you should set the character encoding using the setCharacterEncoding(String encoding) method of the HttpServletRequest object.

How is the ServletContext obtained in a servlet?

  • Using the getServletConfig() method of the HttpServletRequest object.
  • Using the getServletConfig() method of the ServletContext object.
  • Using the getServletContext() method of the HttpServletRequest object.
  • Using the getServletContext() method of the ServletConfig object.
The getServletContext() method of the HttpServletRequest object is used to obtain the ServletContext in a servlet.