ColdFusion和JSP互传数据的两个例子。共同学习。
1。Calling a JSP page from a ColdFusion page
The following page sets Request, Session, and application variables and calls a JSP page, passing it a name parameter:
<cfapplication name="myApp" sessionmanagement="yes">
<cfscript>
Request.myVariable = "This";
Session.myVariable = "is a";
Application.myVariable = "test.";
GetPageContext().include("hello.jsp?name=Bobby");
</cfscript>
The hello.jsp page is called by the CFML. It displays the Name parameter in a header and the three variables in the remainder of the body.
<%@page import="java.util.*" %>
<h2>Hello <%= request.getParameter("Name")%>!</h2>
<br/>Request.myVariable: <%= request.getAttribute("myvariable")%>
<br/>session.myVariable: <%= ((Map)(session.getAttribute("myApp")))
.get("myVariable")%>
<br/>Application.myVariable: <%= ((Map)(application.getAttribute ("myApp"))).get("myVariable")%>
2。Calling a ColdFusion page from a JSP page
The following JSP page sets Request, Session, and application variables and calls a ColdFusion page, passing it a name parameter:
<%@page import="java.util.*" %>
<% request.setAttribute("myvariable", "This");%>
<% ((Map)session.getAttribute("myApp")).put("myVariable", "is a");%>
<% application.setAttribute("myApp.myvariable", "test.");%>
<jsp:include page="hello.cfm">
<jsp:param name="name" value="Robert" />
</jsp:include>
The following hello.cfm page is called by the JSP page. It displays the Name parameter in a heading and the three variables in the remainder of the body.
<cfapplication name="myApp" sessionmanagement="yes">
<cfoutput>
<h2>Hello #URL.name#!</h2>
Request.myVariable: #Request.myVariable#<br/>
Session.myVariable: #Session.myVariable#<br/>
Application.myVariable: #Application.myVariable#<br/>
</cfoutput>