1. What is the main purpose of an HttpSession in a web application?
Why: An HttpSession lets the server remember data about one client between several requests.
2. Can HttpSession be used as a parameter of a Spring MVC controller method?
Why: Spring MVC supports HttpSession as a handler method argument and provides the current session automatically.
3. Can the HttpSession argument passed to a controller method be null?
Why: Spring always provides a session object. If the request has no session yet, a new one is created.
4. What happens if a request does not have a session and the controller method has an HttpSession argument?
Why: Spring gets the session from the request. If none exists, a new session is created, so the argument is never null.
5. Which Servlet API method does Spring use in the background to get the HttpSession argument?
Why: Spring calls HttpServletRequest#getSession(). It returns the current session, or creates a new one if there is none.
6. In Spring 6 and later, which is the correct type to use for a session argument in a controller method?
Why: Spring 6 and later use the Jakarta Servlet API, so the session type is jakarta.servlet.http.HttpSession.
7. Which HttpSession method is used to store a value in the session?
Why:setAttribute() stores a value in the session under a given name.
8. Which HttpSession method is used to read a value from the session?
Why:getAttribute() returns the value stored in the session under a given name.
9. What does getAttribute() return when no value was stored under the given name?
Why: If the name is not in the session, getAttribute() returns null. Your code should check for this.
10.HttpSession#getAttribute() returns an Object. What should you usually do before using the value as a specific type like Long?
Why: Session attributes are stored as Object, so you cast the value to the type you expect.
11. Two different users send requests to the same web application. How are their sessions handled?
Why: Each client gets its own session, so session data of one user is not visible to another user.
12. How long does data stored in an HttpSession normally stay available?
Why: Session data stays available for later requests of the same client until the session times out or is invalidated.
13. Which low-level option can you use in Spring MVC to keep session data if you cannot use session scoped beans?
Why:jakarta.servlet.http.HttpSession comes from the low-level Servlet API and can be used directly in controller methods to maintain session attributes.
14. Which HttpSession method returns the unique identifier of the session?
Why:getId() returns the unique session ID. Requests that belong to the same session have the same ID.