Download JSP with CFML

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Blend of ColdFusion and Java
•
•
If you use ColdFusion (or another Java-based CFML runtime), you should be using
Java. There's a reason that CF uses Java under the hood: Java is incredibly
powerful.
The interface to Java from the CF level is cumbersome and creating hybrid CF/Java
applications pretty much costs you CF's RAD capabilities, but there are some real
gems in the Java libraries
•
.
Using ColdFusion means leveraging the power of Java. ColdFusion encapsulates a
large percentage of the Java web universe and makes it available to you through
native tags, but a vast array of additional functionality is also available through the
use of Java Libraries.
Arrays
• Reversing an array.
There's not a built-in function for doing that, but if you remember that CF arrays are just Java
Lists (java.util.Vector, specifically), you can suddenly leverage the full Java Collections
framework.
arraySort() CF built-in, only sorts text and numbers. So if you want to sort an array of Dates,
you're stuck. Collections.sort, on the other hand, will happily sort the dates.
<cfscript> a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
writeOutput("Original array....<br>");
writeOutput(a.toString());
// reverse it
writeOutput("<br><br>Reverse....<br>");
createObject("java", "java.util.Collections").reverse(a);
writeOutput(a.toString()); // [5, 4, 3, 2, 1, 5, 4, 3, 2, 1]
// sort it
writeOutput("<br><br>Sort....<br>");
createObject("java", "java.util.Collections").sort(a);
writeOutput(a.toString()); // [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] </cfscript>
File Ops
•
CFDIRECTORY ACTION="list" is really slow if you just need a list of filenames
without any of the extra metadata. Using the list() method of the java.io.File
class turned out to be best option
Strings
•
•
•
String is used to manipulate character strings that cannot be changed (read-only and
immutable).
StringBuffer is used to represent characters that can be modified.
Performance wise, StringBuffer is faster when performing concatenations. This is
because when you concatenate a String, you are creating a new object (internally)
every time since String is immutable.
•
•
•
Question :
<cfset test = “ValueLabs” >
What type is test string ?
Strings
<cfset strValue = "This is my ColdFusion string value." />
<cfset strValue = strValue.Trim() />
<cfset strValue = strValue.ReplaceAll( ... ) />
<cfset strValue = strValue.ReplaceFirst( ... ) />
<cfset arrParts = strValue.Split( ... ) />
In Java
<cfset objString = CreateObject("java","java.lang.String").Init(JavaCast("string","I am a
ColdFusion string in Java"))/>
At this point, objString is now a Java String and we
can use it's object methods to manipulate the inner value.
--->
<cfset objString = objString.ReplaceFirst(JavaCast( "string", "ColdFusion" ),JavaCast(
"string", Superb ColdfFusion" )) />
Question
• Which regex engine ColdFusion uses?
Regular expressions
•
•
CF use Jakarta ORO v2.0.6 - changing it for the existing functions would break
backwards compatibility, since other regex engines use different syntax (for both
matching and replacement).
However, you can access the java.util.regex package from CF
Calling Java Class/API
•
•
•
Resolving ambiguous data types with the JavaCast function
The following example shows the use of the JavaCast function to cast arrays:
You have two functions with signatures with the same number of parameters, and a parameter
takes different types of Arrays in different signatures; for example, if you have both of the following
functions: foo(int[] x) and foo(String[] strs).
public class fooClass {
public fooClass () { }
public String foo(long[] arg) {
return "Argument was a long array"; }
public String foo(int[] arg) {
return "Argument was an Integer array"; } }
•
To be able to use these functions in your CFML, use the JavaCast function to convert
the ColdFusion Arrray to the array type required by one of the functions, as shown in
the following code snippet:
<cfset arr = [1,2,4,20,10]>
<cfset fooObj = createObject("java", "fooClass")>
<cfset fooObj.foo(javacast("int[]", arr))>
<cfset fooObj.foo(javacast("long[]", arr))>
Exception Handling
•
ColdFusion provides its own Exceptional handling mechanism. Whenever a ColdFusion page faces
any runtime or logical error or exception, it stops execution of code and transfers control to
ColdFusion’s Exception handling tags.
•
Java’s Exceptions can be caught in the same way as ColdFusion’s with its own tags, but the
Exception handler class must be specified to handle the exception and terminate the piece of code,
or recover from errors gracefully.
•
ColdFusion provides two tags which looks quite similar to Java architecture, < cftry > and < cfcatch
>. But their usage is bit different. In order to handle Java Exceptions in a CFML page, < cftry > and
<cfcatch> could be used. The Name of the Exception class has to be specified in the cfcatch tag
that handles the exception
<cftry>
<cfset cust = createObject (“java" , "Customer" )>
<cfset isInserted = cust.insert (customerBean)>
<cfcatch type="java.lang.Exception.SQLException">
Customer could not be Added
</cfcatch>
<cfcatch type="java.lang.RuntimeException.NullPointerException">
Customer can not be added
</cfcatch>
</cftry>
JSP with CFML
<cfapplication name="myApp" sessionmanagement="yes">
<cfscript>
GetPageContext().include("hello.jsp?name=Sri");
</cfscript>
Hello.jsp
<%@page import="java.util.*" %>
<h2>Hello <%= request.getParameter("name")%>!</h2> <br>
Uses the GetPageContext function to get the current 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
•
•
•
<jsp:include page="hello.cfm">
<jsp:param name="name" value=“Sri" />
</jsp:include>
Thank you!