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.
Which method is used to set a response header with a String value in a servlet?
- addHeader(String name, String value)
- appendHeader(String name, String value)
- setHeader(String name, String value)
- writeHeader(String name, String value)
The setHeader(String name, String value) method is used to set a response header with a String value in a servlet.
Describe the difference between session cookies and persistent cookies.
- Persistent cookies are often used for user authentication
- Persistent cookies are temporary and expire after the browser is closed
- Session cookies are stored permanently on the client-side
- Session cookies expire after a specified time
Session cookies are temporary and expire when the browser is closed, while persistent cookies are stored on the client-side for a longer duration, typically with an expiration date set by the 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.