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.
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.
ServletContext allows servlets to __________ resources and information.
- access
- initialize
- restrict
- share
ServletContext allows servlets to share resources and information.
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.
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.
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.
To define the order of loading for servlets, the __________ element is used in the servlet configuration.
- load-on-startup
- load-order
- order-on-startup
- servlet-order
The load-on-startup element is used in the servlet configuration to define the order of loading for servlets.