Section 13.10. ColdFusion MX and J2EE Integration

   

13.10 ColdFusion MX and J2EE Integration

This section contains a couple of notes with regard to ColdFusion MX that will help us understand the connection between CFMX and the J2EE technologies it employs.

Note that what we have done above is manually create the web.xml file. CFMX also uses such a file, but it creates the file automatically when we place a JSP into a folder under the CFMX/ wwwroot and execute it for the first time. A CFMX-generated web.xml looks slightly different, like this:

 <?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"     "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>     <session-config>         <session-timeout>             30         </session-timeout>     </session-config>     <welcome-file-list> <welcome-file>             index.jsp </welcome-file> <welcome-file>             index.html </welcome-file> <welcome-file>             index.htm </welcome-file> </welcome-file-list> </web-app> 

The welcome-file s serve the same purpose as the welcome file list in IIS, so that the directory can be accessed without a complete filename and extension.

We find something interesting in the ColdFusion MX Web root: it also contains a WEB-INF folder, and it also contains a web.xml file. What do you notice about this snippet from <CFMX_HOME>\wwwroot\WEB-INF\web.xml ?

 <servlet>  <servlet-name>CFCServlet</servlet-name> <display-name>CFC Processor</display-name> <description>Compiles and executes CF web components </description> <servlet-class>coldfusion.xml.rpc.CFCServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CFCServlet</servlet-name> <url-pattern>*.cfc</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CFCServlet</servlet-name> <url-pattern>*.cfc/*</url-pattern> </servlet-mapping> 

The ColdFusion Components (CFCs) you write are translated into servlets. The CFMX application server picks up any .cfc extension in your application path and executes it as a servlet. So, while you create your own servlets that are specifically mapped to a class, CFCs are all mapped to the CFCServlet class in the coldfusion.xml.rpc package.

13.10.1 ColdFusion Page Contexts

Because CFML pages are Java servlets underneath, all ColdFusion pages expose an object called the JavaPageContext . To take advantage of this page context, use the new GetPageContext() CFML function. There are a number of fields and methods that this context makes available. You can access, for instance, the include() function and the forward() function. These functions are one way to integrate JSP and CFML pages.

To use the include() function to include a JSP within your ColdFusion page, call it from within CFScript:

 GetPageContext().include("somePage.jsp"); 

To use the forward() function, which is similiar to the JSP <jsp:forward> tag or the <cflocation> tag, use <cfscript> again. This example passes a parameter name and value to the forwarded page:

 GetPageContext().forward("somePage.jsp?theID=5"); 

13.10.2 Sharing Scopes Between CFML and JSP

You can share three scopes between JSP and CFML: the request, session, and application scopes. You cannot share variables in other scopes, such as URL, FORM, or CFFILE, because JSP does not have a corresponding definition for such scopes.

This example demonstrates how you can use parameters defined in a CFML page and pass them to a JSP page.

13.10.3 application.cfm

 <!---  File: application.cfm Purpose: we have to create a named application  in order to be able to reference shared scopes  with JSP ---> <cfapplication name="myApp" sessionmanagement="yes"> 

13.10.4 scopetest.cfm

 <!---File: scopetest.cfm  Purpose: show sharing between scopes and moving between JSP and CFML pages---> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional// EN"> <HTML>   <HEAD>     <TITLE></TITLE>   </HEAD>   <BODY> <h1>CFML Page</h1> <cfscript> request.cfRequestVar = "I am a CF request var";   session.cfSessionVar = "I am a CF session var";   application.cfApplicationVar = "I am a CF application var"; </cfscript> <cfoutput>#request.cfrequestvar#</cfoutput><br> <cfoutput>#session.cfsessionvar#</cfoutput><br> <cfoutput>#application.cfapplicationvar#</cfoutput><br> <hr> <cfscript>  GetPageContext().include("scopetest.jsp?somekey=somevalue"); </cfscript>   </BODY> </HTML> 

13.10.5 scopetest.jsp

 <%--  File: scopetest.jsp Purpose: set some application and session variables, then forward to a CFML page --%> <%@page import="java.util.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>some JSP</title> </head> <body> <h1>JSP Page</h1> <% // set a variable with the name USER and the value Eben // into the JSP session scope     session.setAttribute("JspUser", "Eben"); // set a variable into the application scope application.setAttribute("JspDB", "javaforcf"); %> JspUser: <%= session.getAttribute("JspUser") %> <br> <br> JspDB: <%= application.getAttribute("JspDB") %> <br> <br>CF application: <%= ((Map)(application.getAttribute("myApp"))).get("cfApplication Var")%> <hr> <jsp:include page="athird.cfm"> <jsp:param name="name" value="dude" /> </jsp:include> </body> </html> 

13.10.6 athird.cfm

 <!---  File: athird.cfm Purpose: display a parameter passed to the page that was created in a JSP (scopes.jsp). ---> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>a third cfml</title> </head> <body> <h1>A different CFML Page</h1> Request: <cfoutput>#name#</cfoutput> </body> </html> 

The thing you'll notice right off the bat is that the Java data type used to store CFML request, session, and application variables is a Map . You know that because we cast the object to this type when calling it. Remember that Map is a kind of Collection, which is why we need to import Java.util.*; .

Calling http://localhost:8500/chp13/scopetest.cfm should produce the output shown in Figure 13.6.

Figure 13.6. Sharing scopes and moving between JSP and CFML.

graphics/13fig06.gif


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net