ColdFusion and JSP


JSP is a tag-based language much like ColdFusion or Active Server Pages. Developers work with or create their own JSP tags in much the same way that ColdFusion developers do. ColdFusion enables you to directly invoke JSP pages, share various forms of variable scopes, pass standard HTTP data types to each other, and hyperlink to each other.

The most common situation in which you will work with JSP and ColdFusion is when calling a JSP page from a ColdFusion template or when calling a ColdFusion template from a JSP page.

For example, if you want to access a JSP page called hello.jsp from a ColdFusion template, you could do something like what is shown in Listing 22.1.

Listing 22.1 getjsp.cfm
 <cfscript>  GetPageContext().include("hey.jsp");  </cfscript> 

In this CFM template, we use a special function called GetPageContext with either the forward or include method. The methods work just like they do in JSP because ColdFusion pages are J2EE Servlet pages, and thus all ColdFusion pages have an underlying Java PageContext object as well. CFML includes the GetPageContext object that you can use with JSPs and Servlets.

If you want to pass a URL parameter to the same page, you would just do this:

 <cfscript>  GetPageContext().include("hey.jsp?name=value");  </cfscript> 

Your ColdFusion and JSP pages can also share data that's contained in the application, request, and session scopes. To do this, however you need to make sure you have the Use J2EE session variables option selected in your ColdFusion Administrator.

Look at the example shown in Listing 22.2.

Listing 22.2 testvars.cfm
 <cfapplication name="testscope" sessionmanagement="yes">  <cflock scope="application" type="exclusive" timeout="10">    <cfset application.appvar = "This is an Application Scoped Var">  </cflock>  <cfset request.reqvar = "This is a Request Scoped Var">  <cflock scope="session" type="exclusive" timeout="10">    <cfset session.sessionvar = "This is a Session Scoped Var">  </cflock>  <cfscript>    GetPageContext().include('testvars.jsp?name=Robi%20Sen');  </cfscript> 

Now let's create a JSP page, as shown in Listing 22.3, to grab all these variables we have set.

Listing 22.3 testvars.jsp
 <%@page import="java.util.*" %>  <h2>URL param passes was <%= request.getParameter("name")%>!</h2>  <br>request.myVariable: <%= request.getAttribute("reqvar")%>  <br>session.myVariable: <%=  ((Map)session.getAttribute("testscope")).get("sessoinvar")%>  <br>application.myVariable: <%= application.getAttribute("application.appvar")%> 

So, if you call the CFML template in your browser, you see that the variables have been passed.

It's just as easy to do the opposite as well. For example, to do the same thing but call a CFML template from JSP, we could follow the same pattern to do what is shown in Listing 22.4.

Listing 22.4 cfmlvarsfromjsp.jsp
 <%@page import="java.util.*" %>  <% request.setAttribute("reqvar", "This is a Request Scoped Var");%>  <% ((Map)session.getAttribute("testscope")).put("sessoinvar", "This is a session  Scoped Var");%>  <% application.setAttribute("testscope.appvar", "This is a application Scoped Var");%>  <jsp:include page="getvarsfromjsp.cfm">     <jsp:param name="name" value="Robi Sen" />  </jsp:include> 

To get the variables from the JSP page, we would just use the code shown in Listing 22.5.

Listing 22.5 getvarsfromjsp.cfm
 <cfapplication name="testscope" sessionmanagement="yes">  <cfoutput>  <h2>URL param passed from JSP is #URL.name#!</h2>  Request.myVariable: #Request.myVariable#<br>  Session.myVariable: #Session.myVariable#<br>  Application.myVariable: #Application.myVariable#<br>  </cfoutput> 

As you can see, it's easy enough to work with JSPs, and you can use the exact same approach to working with Servlets. Finally, you have another option when trying to work with JSPs and Servlets you can use CFHTTP to pass form or URL variables as defined in Chapter 16, "Further Extending Your Applications."

Using Tag Libraries

JSP offers developers a paradigm very similar to ColdFusion for extending the base JSP tag set. Just like ColdFusion, in JSP you can create custom tags, and JSP developers often create whole libraries that encapsulate functionality. (Check out the JRun exchange at www.macromedia.com to see downloadable JSP tag libraries.) To use JSP tag libraries in your ColdFusion applications, you can import them by putting the tag library consisting of the taglibname.jar file and the taglibname.tld file in the web_root/WEB-INF/lib directory.

In the ColdFusion template where you are going to call your JSP tags, you need to specify the tag library name in a cfimport tag. For example:

 <cfimport taglib="/WEB-INF/lib/customtags.jar" prefix="customtags"> 

Mixing JSPs and ColdFusion

In ColdFusion MX, you can actually mix JSP tags inside ColdFusion templates but not inside your CFML. For example, you can do something like this:

 <cfimport taglib="/WEB-INF/lib/jruntags.jar" prefix="jrun" >  <jrun:sql datasrc="/books/2/197/1/html/2/icf" >  Select product_name from product  </jrun:sql>  <cfinclude template="helloworld.jsp" >  <cfscript>  getPageContext().include("helloworld.jsp");  </cfscript> 

Although this provides you with access to powerful JSP tag libraries directly in your ColdFusion applications, it can become very confusing to developers who are not familiar with JSP and Java. So make sure you use JSP and Java appropriately in your development team.



Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579

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