Its an interview question: What is the parent of session object?
As per scope(in image below) my answer 开发者_StackOverflow社区is : application
But, the answer was given as request.. as we can access session object from request context by request.getSession() method.
I am not able to understand how a single request can be parent of whole user session?
Sorry if i am breaking any question guidelines.
Well, there is no parent-child relationship at all here. At least in OO parlance, parent-child relationship is said to be present when there is inheritance involved. Neither Application nor Request fall on the inheritance tree of Session.
So, I would say that the interview question itself is a bit weird :)
The question is strange to me because we don't have relationship in OO meaning. It's only owner (creator) here. We have Http Request implements HttpServletRequest which has:
/**
* Returns the current session associated with this request,
* or if the request does not have a session, creates one.
*/
public HttpSession getSession();
This method will create new object if doesn't exist else it returns created Session Object so if someone can be in parent-relationship, that can be only Request - but in my opinion we can't say that, better question would be: "which class manage http object?" or "which class has http object?"
精彩评论