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.
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.
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.
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.
Session tracking using ________ involves storing a small piece of information on the client's browser.
- Cookies
- Hidden Fields
- Session ID
- URL Rewriting
Session tracking using Cookies involves storing a small piece of information on the client's browser.
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.
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.
What is the impact of calling request.getParameter() after request.getInputStream() or request.getReader() in a servlet?
- It will result in a compilation error.
- It will retrieve the parameter value successfully.
- It will return null.
- It will throw a runtime exception.
Calling request.getParameter() after request.getInputStream() or request.getReader() will return null because the input stream or reader can be consumed only once, and they might have already been read.
How does the servlet container handle thread safety in the case of servlets?
- By creating a new thread for each request.
- By making the service() method synchronized.
- By using a single thread for all requests.
- Thread safety is the responsibility of the developer.
The servlet container handles thread safety by making the service() method synchronized to ensure that only one thread executes it at a time, preventing race conditions in shared resources.
ServletContext allows servlets to __________ resources and information.
- access
- initialize
- restrict
- share
ServletContext allows servlets to share resources and information.
A servlet needs to handle a file upload from a web form. Which HTTP method and content type should be used?
- DELETE with text/plain
- GET with application/x-www-form-urlencoded
- POST with multipart/form-data
- PUT with application/json
For handling file uploads, the appropriate combination is to use the POST method along with the multipart/form-data content type.
Servlets can be used to handle ______, while JSP is used for presenting these to the users.
- HTTP requests
- business logic
- database operations
- presentation
Servlets are commonly used for handling business logic, database operations, etc., while JSP is primarily used for presenting content to users.