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:
<cfapplication name="myApp" sessionmanagement="yes"> <cfscript> Request.myVariable = "This"; Session.myVariable = "is a"; Application.myVariable = "test."; GetPageContext().include("hello.jsp?name=Bobby"); </cfscript>
The following table describes the CFML code and its function:
Code |
Description |
---|---|
<cfapplication name="myApp" sessionmanagement="yes"> |
Specifies the application name as myApp and enables session management. In most applications, this tag is in the Application.cfm page. |
<cfscript> Request.myVariable = "This"; Session.myVariable = "is a"; Application.myVariable = "test."; |
Sets ColdFusion Request, Session, and Application, scope variables. Uses the same name, myVariable, for each variable. |
GetPageContext().include ("hello.jsp?name=Bobby"); </cfscript> |
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 hello.jsp page is called by the ColdFusion 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):
Code |
Description |
---|---|
<%@page import="java.util.*" %> |
Imports the java.util package. This contains methods required in the JSP page. |
<h2>Hello <%= request.getParameter ("name")%>!</h2> |
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. |
<br>request.myVariable: <%= request. getAttribute("myvariable")%> |
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 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 variable will not get its value ColdFusion page. |
<br>session.myVariable: <%= ((Map)(session.getAttribute("myApp")) ).get("myVariable")%> |
Uses the getAttribute method of the JSP session object to get the myApp object (the Application scope). Casts this 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. |
<br>Application.myVariable: <%=((Map)(application.getAttribute("m yApp"))).get("myVariable")%> |
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:
<%@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>
The following table describes the JSP code and its function:
Code |
Description |
---|---|
<%@page import="java.util.*" %> |
Imports the java.util package. This contains methods required in the JSP page. |
<% request.setAttribute("myvariable", "This");%> |
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 will not share it with the ColdFusion page. |
<% ((Map)session.getAttribute("myApp")).put( "myVariable", "is a");%> |
Uses the getAttribute method of the JSP session object to get the myApp object (the Application scope). Casts this 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. |
<% ((Map)application.getAttribute("myApp")). put("myVariable","test.");%> |
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. |
<jsp:include page="hello.cfm"> <jsp:param name="name" value="Robert" /> </jsp:include> |
Sets the name parameter to Robert and calls the ColdFusion page hello.cfm. |
The following hello.cfm page is called by the JSP 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:
Code |
Description |
---|---|
<cfapplication name="myApp" sessionmanagement="yes"> |
Specifies the application name as myApp and enables session management. In most applications, this tag is in the Application.cfm page. |
<cfoutput> <h2>Hello #URL.name#!</h2> |
Displays the name passed using the jsp:param tag on the JSP page. The parameter name is not case-sensitive. |
Request.myVariable: #Request.myVariable#<br> Session.myVariable: #Session.myVariable#<br> Application.myVariable: #Application.myVariable#<br> </cfoutput> |
Displays the Request.myVariable, Session. myVariable, and Application.myVariable values. Note that all variable names on CFML pages are case independent. |