When I was learning how to create a session in Servlets. I read that Servlet API provides a class called HttpSession to create sessions.
HttpSession session = request.getSession(true);
Then, I was looking at some sample codes and saw this piece of code.
// Get the bean from session.
Customer customer = request.getSession(true).getAttribute("userinfo");
What exactly is happening here? How did they create a session without using the HttpSession class?
The line
is equivalent to
but without the temporary variable. It’s just method chaining.
request.getSession(true)is an expression whose type isHttpSession, since thegetSession()method returns aHttpSession. So you can call methods ofHttpSessionon this expression.Note that
requestis an HttpServletRequest, and not anHttpServletas your question says.