To access the MIME type of a file, use the __________ method of ServletContext.
- analyzeMimeType()
- fetchMimeType()
- getMimeType()
- retrieveMimeType()
To access the MIME type of a file, use the getMimeType() method of ServletContext.
In a scenario where form data includes special characters like & and %, which approach ensures accurate data retrieval?
- Apply Base64 encoding to the form data
- HTML-escape the special characters
- URLEncode the form data before processing
- Use the raw form data directly
URLEncoding the form data before processing ensures accurate data retrieval, especially when dealing with special characters like & and %. This encoding prevents issues with parsing and processing the data correctly.
In a scenario where a cookie is used for authentication, what measures should be taken to enhance security?
- All of the above
- Encrypt the cookie data
- Store minimal information in the cookie
- Use HTTPS
To enhance security in cookie-based authentication, it's crucial to use HTTPS, store minimal information, and encrypt the cookie data.
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.
A servlet needs to load database settings at startup. How should these settings be passed to the servlet?
- Hardcode the settings in the servlet code
- Store settings in a local properties file
- Use ServletContext parameters
- Use request parameters
The recommended approach is to use ServletContext parameters to pass settings at servlet startup, as they can be accessed by all servlets within the same web application. Hardcoding or using request parameters may not be suitable for initialization settings.
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.
How does the HttpOnly attribute enhance the security of a cookie?
- Allows the cookie to be modified by client-side scripts
- Enables the cookie to be accessed by JavaScript
- Prevents client-side scripts from accessing the cookie
- Restricts the cookie to HTTP connections
The HttpOnly attribute enhances cookie security by preventing client-side scripts from accessing the cookie. This helps mitigate the risk of cross-site scripting (XSS) attacks that aim to steal sensitive information from cookies.