In servlets, which method is used to add a cookie to the response headers?
- response.addCookie(Cookie cookie);
- response.createCookie(Cookie cookie);
- response.insertCookie(Cookie cookie);
- response.setCookie(Cookie cookie);
The correct method to add a cookie to the response headers in servlets is response.addCookie(Cookie cookie);.
What happens if a non-existent initialization parameter is requested in a servlet?
- The servlet will automatically create the parameter with a default value.
- The servlet will prompt the user to provide the missing parameter.
- The servlet will receive a null value for the requested parameter.
- The servlet will throw a runtime exception.
If a servlet requests a non-existent initialization parameter, it will receive a null value for the requested parameter. Servlets should handle null values appropriately to avoid unexpected behavior.
How does an HTTP servlet handle multipart/form-data requests?
- It accesses the data using request.getParts() method.
- It employs the request.getInputStream() method.
- It uses the HttpServletRequest.getParameter() method.
- It utilizes the MultipartRequest class.
Handling multipart/form-data requests in an HTTP servlet involves using the request.getParts() method to access the various parts of the request, allowing extraction of file uploads and other data.
Which Java method is used to add a cookie to the response object?
- addCookie()
- createCookie()
- insertCookie()
- setCookie()
The addCookie() method is used in Java to add a cookie to the response object and send it to the client.
A servlet is loaded at startup, processes several requests, and then is removed from the server. Identify the correct order of method invocations in its lifecycle.
- destroy() -> service() -> init()
- init() -> destroy() -> service()
- init() -> service() -> destroy()
- service() -> init() -> destroy()
The correct order of method invocations in the lifecycle of a servlet is init() (initialization) -> service() (processing requests) -> destroy() (removal from the server).
If a servlet needs to log application-wide events, which object would be the best choice for retrieving the log file’s path?
- HttpServletRequest
- HttpServletResponse
- ServletConfig
- ServletContext
The ServletContext provides a way to retrieve information that is shared among servlets, making it suitable for obtaining the log file's path for application-wide event logging.
What technology is typically used for writing presentation logic in web applications?
- Applet
- JSP
- JavaBean
- Servlet
JavaServer Pages (JSP) is typically used for writing presentation logic in web applications, allowing Java code to be embedded into HTML for dynamic content generation.
How does cookie-based session tracking work in servlets?
- Cookies are not used for session tracking in servlets.
- Cookies are used only for authentication in servlets.
- Cookies are used to store session information on the client side.
- Cookies are used to store session information on the server side.
Cookie-based session tracking in servlets involves storing session information on the client side using cookies. This allows the server to recognize and associate subsequent requests with the same session.
How does the HttpSession interface facilitate session tracking?
- It allows direct manipulation of the HTTP response.
- It enables the creation and management of sessions.
- It is responsible for URL rewriting in session tracking.
- It provides methods to manipulate cookies.
The HttpSession interface in servlets facilitates session tracking by enabling the creation and management of sessions. It provides methods to store, retrieve, and manage session attributes for a particular client.
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.