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.
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");
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.
<!--- 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">
<!---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>
<%-- 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>
<!--- 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.
Top |