简体   繁体   English

我如何在jsp文件中检索会话对象(由servlet存储)?

[英]how do i retrieve a session object(stored by servlet) in a jsp file?

Is it possible to retrieve session object stored by a servlet, in a JSP file? 是否可以在JSP文件中检索servlet存储的会话对象? How do I do that? 我怎么做?

You can use EL ${} in JSP to access objects in page, request, session and application scope by their attribute name. 您可以在JSP中使用EL ${}来访问页面,请求,会话和应用程序范围内的对象的属性名称。 You just have to specify the same name as you used in the servlet to store the attribute. 您只需指定与servlet中用于存储属性的名称相同的名称。 For example, when you store an User object with the attribute name "user" as follows 例如,当您存储具有属性名称"user"User对象时,如下所示

request.getSession().setAttribute("user", user);

then it's available in the forwarded JSP by the same attribute name as follows 然后它在转发的JSP中可用相同的属性名称,如下所示

${user}

Another example if it has a name property with a getter: 另一个例子,如果它有一个带有getter的name属性:

<p>Welcome, <c:out value="${user.name}" /></p>

See also: 也可以看看:

What handles the JSP? 什么处理JSP? Anyway, if you use any more or less decent version of EL, you should be able to get it from EL via the implicit session object, like ${session.objectName} . 无论如何,如果你使用任何或多或少不错的EL版本,你应该能够通过隐式session对象从EL获取它,比如${session.objectName}

这篇文章给出了JSP和servlet的会话访问的相当全面的解释。

通过EL: ${sessionScope.myObject}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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