ServletConfig is used to pass __________ to a specific servlet during initialization.
- configuration
- context
- parameters
- resources
ServletConfig is used to pass initialization parameters to a specific servlet during initialization.
The _________ method of HttpServletRequest is used to parse query parameters from the request URL.
- getParameter()
- getQueryParameters()
- parseQueryParameters()
- retrieveQuery()
The getParameter() method of HttpServletRequest is used to parse query parameters from the request URL.
How is ServletConfig initialized?
- Automatically by the container
- Using a configuration file
- Using constructor
- Using init() method
The ServletConfig is automatically initialized by the container. It provides configuration information to the servlet and can be obtained using the getServletConfig() method.
What is the difference between context parameters and initialization parameters in servlets?
- Context parameters are set at the application level, while initialization parameters are specific to a servlet.
- Context parameters are used for database connectivity, while initialization parameters are used for servlet configuration.
- Initialization parameters are set at the application level, while context parameters are specific to a servlet.
- Initialization parameters are used for database connectivity, while context parameters are used for servlet configuration.
Context parameters are set at the application level and are accessible to all servlets, while initialization parameters are specific to each servlet and are defined in the servlet's deployment descriptor (web.xml).
The response header 'Content-Disposition' with value 'attachment; filename="file.txt"' is set using the __________ method.
- addHeader()
- sendRedirect()
- setContentType()
- setHeader()
The setHeader() method is used to set response headers, including the 'Content-Disposition' for file downloads.
How can you securely send sensitive data from a client to a server in a web application?
- Encode data in Base64
- Send data in plain text
- Use HTTP with custom encryption
- Use HTTPS (SSL/TLS)
Sensitive data should be sent securely, and using HTTPS (SSL/TLS) ensures encrypted communication between the client and the server, providing a secure way to transmit sensitive information.
How are HTTP session cookies handled in servlets?
- Cookies are handled automatically by servlet containers.
- Cookies are managed using the Cookie class.
- Cookies are not supported in servlets.
- Cookies must be handled manually in the doGet() method.
In servlets, HTTP session cookies are typically managed using the Cookie class, allowing developers to handle cookie creation, retrieval, and manipulation programmatically.
The __________ method of the ServletContext interface is used to retrieve context parameters.
- contextParam()
- getContextParameter()
- getInitParameter()
- retrieveParameter()
The getInitParameter() method of the ServletContext interface is used to retrieve context parameters associated with the servlet's context.
In a servlet's lifecycle, which method is responsible for responding to client requests?
- doGet()
- doPost()
- init()
- service()
The service() method in a servlet's lifecycle is responsible for responding to client requests. It delegates the request to the appropriate method (e.g., doGet() or doPost()).
In a web application, a servlet receives data, processes it, and then needs to display the results in a JSP page. Describe the optimal approach for this scenario.
- Include the JSP page using
- Set data as request attributes and use request.getRequestDispatcher().forward()
- Use JavaScript to fetch and display data in the JSP page
- Use response.sendRedirect() to navigate to the JSP page
The optimal approach is to set the processed data as request attributes and use request.getRequestDispatcher().forward() to forward the request to the JSP page. This ensures that the processed data is available in the JSP for rendering.