ColdFusion pages and JSP pages can interoperate in several ways:
- ColdFusion pages can invoke JSP pages and servlets.
- JSP pages can invoke ColdFusion pages.
- ColdFusion pages, JSP pages, and servlets can share data in three scopes.
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 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 in this page.
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 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:
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.
Running Servlets in ColdFusion
ColdFusion runs an application server known as Tomcat. Since all application servers have a web container by default, application servers can run servlets.
To run a servlet on ColdFusion:
1. Create a cfm page and call the servlet. For example, the name of the servlet is testServlet . Use the function GetPageContext() .forward (' testServlet '). This function gets the current ColdFusion PageContext object that provides access to page attributes and configuration, request, and response objects. The cfm page is as follows:
<html>
<head>
<title>Servlets in ColdFusion demo</title>
</head>
<body>
<h1>This cfm page calls the servlet 'testServlet'</h1>
<cfscript>
GetPageContext().forward(“testServlet”);
</cfscript>
</body>
</html>
2. Create a servlet file (testServlet.java).
3. Compile and copy servlet '.Class' file to the locations:
C:\<ColdFusion Installation Directory>\wwwroot\WEB-INF\classes\ for stand-alone installation of ColdFusion.
C:\<App server>\servers\<coldfusion instance name>\cfusion-ear\cfusion-war\WEB-INF\classes\ for ColdFusion in multiple servers.
4. Map the servlet in the file web.xml. This file is present in the following locations:
C:\<ColdFusion Installation Directory>\wwwroot\WEB-INF\ for stand-alone installation of ColdFusion.
C:\<App server>\servers\<coldfusion instance name>\cfusion-ear\cfusion-war\WEB-INF\ for ColdFusion in multiple servers.
5. Add the following code to web.xml
<servlet>
<description></description>
<display-name>testServlet</display-name>
<servlet-name>testServlet</servlet-name>
<servlet-class>testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
You can see the following output:
Accessing ColdFusion application and session variables in JSP pages
ColdFusion runs as a JEE application on a JEE application server. The JEE 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 JEE session (assuming that the J2EE Sessions feature has been enabled, as discussed in the next session.) Because ColdFusion identifies sessions by the application name. the session structure is contained in an attribute of the JEE 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:
Scope |
Can share data using |
---|---|
Request |
forward, include*Note:* Shared Request scope variable names in the JSP page or servlet must be all-lowercase. |
Session |
href, cfhttp, forward, include |
Application |
href, cfhttp, forward, include |
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 in Using Java objects. |
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 in Configuring and using session variables. (While the term "J2EE" has been updated in Java parlance with the simpler JEE, the ColdFusion Administrator still refers to storing sessions in the JEE servlet scope as "J2EE sessions".)
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 Using JSP with CFML below.
Note: When running in the server configuration, ColdFusion also shares the Form scope when calling a JSP or servlet. In the JEE configuration, however, sharing the Form scope is dependent on the JEE application server. ColdFusion always shares the Request, Session, and Application scopes. |
Unnamed ColdFusion Application and Session scopes
If 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 JEE 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 JEE application server. (If you do not use J2EE session variables, ColdFusion ensures that the JEE 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 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:
<cfscript> |
Reviewing the code
The following table describes the CFML code and its function:
Code |
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 method of the page context object to call the hello. jsp page. Passes the name parameter in the URL. |
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.
<h2>Hello <%= request.getParameter("name")%>!</h2> |
The following table describes the JSP code and its function (line breaks added for clarity):
Code |
Description |
|
---|---|---|
|
Imports the java.util package. This package contains methods required in the JSP page. |
|
|
Displays the name passed as a URL parameter from the ColdFusion page. The parameter name is case sensitive,Note: The getParameter request method cannot get all ColdFusion page request parameter values on some application servers. For example, on IBM WebSphere, you cannot use getParameter to get form fields. |
|
|
Uses the getAttribute method of the JSP request object to displays the value of the Request scope variable myVariable .The JSP page must use all lowercase characters to reference all request scope variables that it shares with CFML pages. You can use any case on the CFML page, but if you use mixed case or all uppercase on the JSP page, the variable is not set from the ColdFusion page. |
|
|
Uses the getAttribute method of the JSP session object to get the myApp object (the Application scope). Casts this object to a Java Map object and uses the get method to obtain the myVariable value for display.CFML pages and JSP pages share Session variables independent of the variable name case. The variable on the JSP page can have any case mixture and still receive the value from the ColdFusion page. For example, instead of myVariable , you could use MYVARIABLE or myvariable on this line. |
|
|
Uses the getAttribute method of the JSP myApp application object to obtain the value of myVariable in the Application scope.CFML pages and JSP pages share Application variables independent of the variable name case. The variable on the JSP page can have any case mixture and still receive the value from the ColdFusion page. For example, instead of myVariable , you could use MYVARIABLE or myvariable on this line. |
Calling a ColdFusion page from a JSP page
The following JSP page sets Request, Session, and application variables and calls a ColdFusion page, passing it a name parameter:
|
Reviewing the code
The following table describes the JSP code and its function:
Code |
Description |
|
---|---|---|
|
Imports the java.util package. This package contains methods required in the JSP page. |
|
|
Uses the setAttribute method of the JSP request object to set the value of the Request scope variable myVariable . The JSP page must use all lowercase characters to refer to all request scope variables that it shares with CFML pages. You can use any case on the CFML page, but if you use mixed case to all uppercase on the JSP page, the JSP page does not share it with the ColdFusion page. |
|
|
Uses the getAttribute method of the JSP session object to get the myApp object (the Application scope). Casts this object to a Java Map object and uses the set method to set the myVariable value.CFML pages and JSP pages share Session variables independent of the variable name case. The variable on the JSP page can have any case mixture and still share the value with the ColdFusion page. For example, instead of myVariable , you could use MYVARIABLE or myvariable on this line. |
|
|
Uses the getAttribute method of the JSP application object to get myApp object (the Application scope) and casts it to a Map object. It then sets the value of myVariable in the myApp application scope object.CFML pages and JSP pages share Application variables independent of the variable name case. The variable on the JSP page can have any case mixture and still share the value with the ColdFusion page. For example, instead of myVariable , you could use MYVARIABLE or myvariable on this line. |
|
|
Sets the name parameter to Robert and calls the ColdFusion page hello.cfm . |
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.
<cfoutput> |
The following table describes the CFML code and its function:
Code |
Description |
|
---|---|---|
|
Specifies the application name as myApp and enables session management. In most applications, this tag is in the Application.cfm page. |
|
|
Displays the name passed using the jsp :param tag on the JSP page. The parameter name is not case sensitive. |
|
|
Displays the Request. myVariable , Session. myVariable , and Application.myVariable values. All variable names on CFML pages are case independent.
|
Descargar
Descargar