What method of the HttpServletRequest object is used to retrieve a form parameter by name?
- fetchParameter(String name)
- getFormParameter(String name)
- getParameter(String name)
- retrieveParameter(String name)
The getParameter(String name) method is used to retrieve a form parameter by name from the HttpServletRequest object.
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.
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.