A servlet's URL pattern is specified in the __________ element in the web application's deployment descriptor.
- servlet-mapping
- servlet-url
- url-mapping
- url-pattern
The url-pattern element in the web application's deployment descriptor is used to specify a servlet's URL pattern, mapping it to a particular servlet.
The __________ method invalidates the current session and removes its binding from the context.
- destroySession()
- endSession()
- invalidate()
- removeSession()
The invalidate() method invalidates the current session and removes its binding from the context.
What changes are required in a servlet to handle multipart/form-data requests?
- Implement a custom parsing algorithm
- Implement doPost method
- No changes needed
- Set content type to multipart/form-data
To handle multipart/form-data requests, set the content type to multipart/form-data in the servlet. This allows the servlet to recognize and process multipart requests correctly, especially when dealing with file uploads or other complex data structures.
Cookies used for tracking user sessions are typically called _________ cookies.
- Persistent
- Secure
- Session
- Tracking
Cookies used for tracking user sessions are typically called Session cookies.
In a web application with multiple servlets needing a shared resource, where should this resource be initialized?
- In a Servlet class constructor
- In the contextInitialized() method of ServletContextListener
- In the doGet() method of each servlet
- In the init() method of each servlet
For a shared resource across multiple servlets, it's best to initialize it in the contextInitialized() method of a ServletContextListener, ensuring it's available when the application starts.
What is session tracking in the context of servlets?
- Handling HTTP requests
- Maintaining user sessions
- Managing servlet lifecycle
- Processing form data
Session tracking in servlets involves maintaining user sessions, allowing the server to recognize and remember users across multiple requests, usually achieved through mechanisms like cookies or URL rewriting.
If a servlet needs to access a shared resource specified in the web application's context parameters, which approach should be used?
- Access the shared resource through the HttpSession object.
- Directly access the shared resource using the File class.
- Use a global variable within the servlet to store the shared resource.
- Utilize the ServletContext to retrieve the shared resource using getInitParameter().
To access a shared resource specified in the web application's context parameters, a servlet should use the ServletContext and the getInitParameter() method. This ensures proper retrieval of the shared resource.
The __________ method is called to allow the servlet to respond to a request.
- destroy()
- doGet()
- init()
- service()
The service() method is called to allow the servlet to respond to a request. It handles the actual processing of the request.
A servlet needs to read its own initialization parameters and also share a database connection pool. Which combination of objects should it use?
- getInitParameter() and ServletContext
- getServletConfig() and getServletContext()
- getServletContext() and getInitParameter()
- getServletContext() and getInitParameter()
To read its own initialization parameters, a servlet should use getInitParameter(). To share a database connection pool, it should use the ServletContext, which is accessible via getServletContext().
To set multiple values for the same header, use the __________ method in HttpServletResponse.
- addHeader()
- appendHeader()
- setHeader()
- setMultipleValuesHeader()
The addHeader() method in HttpServletResponse is used to add multiple values for the same header, allowing for flexibility in handling headers with multiple values.
In which method of a servlet are most of the resources like threads, database connections, etc., released?
- destroy()
- doGet()
- init()
- service()
The destroy() method is called when a servlet is being removed from service, and it is used to release resources like threads and database connections.
When using RequestDispatcher's forward method, changes to the response _________ affect the final output.
- always
- may
- will
- will not
When using RequestDispatcher's forward method, changes to the response will not affect the final output, as the forward method transfers control without changing the response.