ColdFusion 9.0 Resources |
Interoperating with JSP pages and servletsContents [Hide]ColdFusion pages and JSP pages can interoperate in several ways:
Integrating JSP and servlets in a ColdFusion applicationYou 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 include and forward methods to call the page. As with any web application, you can use href links in ColdFusion pages to open JSP pages. 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 jsp:forward or jsp:include tag to call the ColdFusion page. For an example of calling a ColdFusion page from a JSP page, see Calling a JSP page from a ColdFusion page. Accessing a JSP page or servlet from a ColdFusion pageTo access a JSP page or servlet from a ColdFusion page, you use the GetPageContext function with the forward or the include method. For example, to include a JSP “Hello World" page in your ColdFusion application, use the following line: 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 pagesColdFusion 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 servletsIf 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:
Note: When you share data between ColdFusion pages
and JSP pages, be careful about data type conversion issues. For
more information, see Java and ColdFusion data type conversions.
To share session variables, specify J2EE session management in the ColdFusion Administrator. For more information on configuring and using J2EE Session scope management, see ColdFusion and J2EE session management. For example, you could put the customer order structure used in the previous example in the Session scope. Then, you would not have to pass the order values as a set of parameters. Instead, the JSP pages could access the Session scope variables directly, and the ColdFusion page would only require a line like the following to call the JSP page: getPageContext().forward(URLEncodedFormat("/responsegen/responsegen.jsp")); For examples of using the Request, Session, and Application scopes to share data between ColdFusion pages and JSP pages, including samples of the appropriate JSP code, see Examples: using JSP with CFML. Note: When running in the server
configuration, ColdFusion also shares the Form scope when calling
a JSP or servlet. In the J2EE configuration, however, sharing the Form
scope is dependent on the J2EE application server. For example,
JRun shares the Form scope, IBM WebSphere does not. ColdFusion always
shares the Request, Session, and Application scopes.
Unnamed ColdFusion Application and Session scopesIf you do not specify an application name in the This.name variable in the Application.cfc initialization code or by using the ColdFusion cfapplication tag, the application is unnamed, and the Application scope corresponds to the ColdFusion J2EE servlet context. ColdFusion, therefore, supports only a single unnamed application. If multiple cfapplication tags and Application.cfc files do not specify an application name, all pages in these applications share the servlet context as their Application scope. All sessions of unnamed applications correspond directly to the session object of the J2EE application server. (If you do not use J2EE session variables, ColdFusion ensures that the J2EE session lasts at least as long as the session time-out.) You access an Application scope variable from a ColdFusion unnamed application in a JSP page using the following format: application.getAttribute("applicationVariableName") You access Session scope variables in a ColdFusion unnamed application as follows: session.getAttribute("sessionVariableName") Note: When you use application and session variables
for the unnamed ColdFusion application in JSP pages and servlets,
the variable names must be case-correct. The characters in the variable
name must have the same case as you used when you created the variable
in ColdFusion. You do not have to use case-correct application and
session variable names for named ColdFusion applications.
Examples: using JSP with CFMLThe 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 pageThe 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 codeThe following table describes the CFML code and its function:
The ColdFusion page calls the hello.jsp page. 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")%> The following table describes the JSP code and its function (line breaks added for clarity):
Calling a ColdFusion page from a JSP pageThe 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");%> <% ((Map)application.getAttribute("myApp")).put("myVariable", "test.");%> <jsp:include page="hello.cfm"> <jsp:param name="name" value="Robert" /> </jsp:include> Reviewing the codeThe following table describes the JSP code and its function:
The JSP page calls the following hello.cfm 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> The following table describes the CFML code and its function:
|