简体   繁体   中英

What's the difference between the methods findAttribute() and getAttribute() in servlet?

If I've set "cart" as a session attribute, then what's the difference between the methods pageContext.findAttribute("cart") and session.getAttribute("cart") in servlet file ? Do they do the same thing in this case?

Was it really too much effort to read the Javadoc for PageContext.findAttribute() ?

The PageContext looks in multiple scopes for the attribute (page, request, session then application) whereas the session.getAttribute() only looks in the session. If you know the attribute is in the session, then session.getAttribute() should be (marginally) faster.

Object findAttribute (String AttributeName) : This method searches for the specified attribute in all four levels in the following order – Page, Request, Session and Application. It returns NULL when no attribute found at any of the level.

session.getAttribute("cart"): This method searches for the specified attribute in only Session scope.

Do they do the same thing in this case? => They do same thing in this case if you dont have attribute name cart in Page, Request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM