Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 1135

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
Integrating JSP and servlets in a ColdFusion application
You can integrate JSP pages and servlets in your ColdFusion application. For example, you can write some application
pages in JSP and write others in CFML. ColdFusion pages can access JSP pages by using the JSP
methods to call the page. As with any web application, you can use
The ability to use JSP lets you incorporate legacy JSP pages in your ColdFusion application, or conversely, use CFML
to expand an existing JSP application using ColdFusion pages.
If you have a JSP page that must call a ColdFusion page, you also use a
ColdFusion page. For an example of calling a ColdFusion page from a JSP page, see
ColdFusion
page" on page 1132.
Accessing a JSP page or servlet from a ColdFusion page
To access a JSP page or servlet from a ColdFusion page, you use the GetPageContext function with the
method. For example, to include a JSP "Hello World" page in your ColdFusion application, use the following line:
include
GetPageContext().include("hello.jsp");
To pass parameters to the JSP page, include the parameters in the page URL.
For example, you could want to integrate an existing JSP customer response component into a new ColdFusion order
processing application. The order processing application provides the order number, total cost, and expected shipping
date, and the customer response component sends the response to the e-mail address on file for the particular
customer number. The ColdFusion application could use the following CFScript code to call the response JSP page:
urlParams =
"UID=#order.uid#&cost=#order.total#&orderNo=#order.orderNo#&shipDate=#order.shipDateNo#"
getPageContext().forward(URLEncodedFormat("/responsegen/responsegen.jsp?#urlParams#"));
To access a servlet that exposes the same functionality, you use the same code, although the URL would change. For
example, to run a servlet called HelloWorldServlet, you place the servlet .java or .class file in the serverroot/WEB-
INF/classes directory and access the servlet with the URL /servlet/HelloWorldServlet.
Accessing ColdFusion application and session variables in JSP pages
ColdFusion runs as a J2EE application on the J2EE application server. The J2EE application ServletContext is a data
structure that stores objects as attributes. A ColdFusion Application scope is represented as an attribute named by the
Application scope name. The attribute contains the scope values as a hash table. Therefore, you access ColdFusion
Application scope variable in a JSP page or servlet using the following format:
((Map)application.getAttribute("CFApplicationName"))).get("appVarName")
Similarly, the ColdFusion Session scope is a structure within the J2EE session. Because ColdFusion identifies sessions
by the application name. the session structure is contained in an attribute of the J2EE session that the application name
identifies. Therefore, you access ColdFusion session variables as follows:
((Map)(session.getAttribute("CFApplicationName"))).get("sessionVarName")
Sharing data between ColdFusion pages and JSP pages or servlets
If an application includes ColdFusion pages and JSP pages or servlets, they can share data in the Request, Session and
Application scopes. The following table lists the ways that you can access JSP pages with which you want to share the
scope data:
links in ColdFusion pages to open JSP pages.
href
jsp:forward
Last updated 1/20/2012
and
include
forward
or
tag to call the
jsp:include
"Calling a JSP page from a
or the
forward
1130

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents