[英]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 ? 如果将“ cart”设置为会话属性,那么servlet文件中的pageContext.findAttribute("cart")
和session.getAttribute("cart")
方法有什么区别? Do they do the same thing in this case? 他们在这种情况下是否做同样的事情?
Was it really too much effort to read the Javadoc for PageContext.findAttribute() ? 阅读PageContext.findAttribute()的Javadoc真的花费了太多精力吗?
The PageContext
looks in multiple scopes for the attribute (page, request, session then application) whereas the session.getAttribute()
only looks in the session. PageContext
在多个作用域中查找属性(页面,请求,会话然后是应用程序),而session.getAttribute()
仅在会话中查找。 If you know the attribute is in the session, then session.getAttribute()
should be (marginally) faster. 如果您知道该属性在会话中,则session.getAttribute()
应该(略微)更快。
Object findAttribute (String AttributeName) : This method searches for the specified attribute in all four levels in the following order – Page, Request, Session and Application. 对象findAttribute(字符串AttributeName) :此方法按以下四个顺序在所有四个级别中搜索指定的属性-页面,请求,会话和应用程序。 It returns NULL when no attribute found at any of the level. 如果在任何级别都没有找到属性,则返回NULL。
session.getAttribute("cart"): This method searches for the specified attribute in only Session scope. session.getAttribute(“ cart”):此方法仅在Session范围内搜索指定的属性。
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. =>如果您在“页面,请求”中没有属性名称购物车,则在这种情况下它们也会执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.