如何将Object从servlet传递给调用JSP [英] How to pass an Object from the servlet to the calling JSP
本文介绍了如何将Object从servlet传递给调用JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将Object从servlet传递给调用JSP。
How to pass an Object from the servlet to the calling JSP.
我有一个JSP调用servlet。从这个servlet,我正在设置viewBean的属性。
现在,我想在JSP页面上从Servlet获取此属性值集。
I have a JSP calling a servlet. From this servlet, I am setting the properties of a viewBean. Now, I want to get this property valued set from Servlet on a JSP page.
如何使用Servlet在JSP上提供此ViewBean对象。
How to make this ViewBean object available on JSP from Servlet.
推荐答案
将对象放在会话或请求中的servlet中:
Put the object either in session or request in servlet like :
String shared = "shared";
request.setAttribute("sharedId", shared); // add to request
request.getSession().setAttribute("sharedId", shared); // add to session
this.getServletConfig().getServletContext().setAttribute("sharedId", shared); // add to application context
您可以在jsp中读取它:
You can read it in jsp like :
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<body>
<cut value= "${shared}"/>
<cut value= "${requestScope.shared}"/>
<cut value= "${requestScope.request.shared}"/>
${shared}
或者使用带有代码的scriptlet读取它:
Or read it using scriptlet with code :
<%
String shared = (String)request.getAttribute("sharedId");
String shared1 = (String)request.getSession().getAttribute("sharedId");
String shared2 = (String)this.getServletConfig().getServletContext().getAttribute("sharedId");
%>
这篇关于如何将Object从servlet传递给调用JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文