Adobe COLDFUSION 9 Manual page 1137

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
Examples: using JSP with CFML
The following simple examples show how you can integrate JSP pages, servlets, and ColdFusion pages. They also show
how you can use the Request, Application, and Session scopes to share data between ColdFusion pages, JSP pages, and
servlets.
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>
Reviewing the code
The following table describes the CFML code and its function:
Code
<cfapplication name="myApp"
sessionmanagement="yes">
<cfscript>
Request.myVariable = "This";
Session.myVariable = "is a";
Application.myVariable = "test.";
GetPageContext().include("hello.jsp?name
=Bobby");
</cfscript>
The ColdFusion page calls the hello.jsp page. It displays 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")%>
The following table describes the JSP code and its function (line breaks added for clarity):
Description
Specifies the application name as myApp and enables session management. In most
applications, this tag is in the Application.cfm page.
Sets ColdFusion Request, Session, and Application, scope variables. Uses the same
name, myVariable, for each variable.
Uses the GetPageContext function to get the current servlet page context for the
ColdFusion page. Uses the
include
hello.jsp page. Passes the name parameter in the URL.
parameter in a header and the three variables in the
name
Last updated 8/5/2010
method of the page context object to call the
1132

Advertisement

Table of Contents
loading

Table of Contents