Attribute
Последна актуализация на
20.01.2022 г.
Description
Provides support for writing Java code within ColdFusion.
Syntax
<cfjava instance="instance" </cfjava>
History
Adobe ColdFusion (2021 release): New in this release.
Attributes
|
Req/Opt |
Description |
---|---|---|
Instance |
Required |
The variable that holds the value of class instance of the Java code. |
Usage
You can create Java classes with a block of CFML code and execute the code. You can instantiate Java objects and write core Java constructs within a CFML block.
Example- Tag
<cfjava handle="classInstance" > import java.io.*; public class Harmless{ private String ss = "John"; public Harmless(String s){ this.ss = s; } public void write(String path) throws Exception{ File file = new File(path); file.createNewFile(); FileWriter fr = new FileWriter(file, true); BufferedWriter br = new BufferedWriter(fr); PrintWriter pr = new PrintWriter(br); pr.println("new content from java added " + this.ss); pr.close(); br.close(); fr.close(); } public String read (String path) throws Exception{ File file = new File(path); BufferedReader objReader = null; String strCurrentLine; String cont = ""; objReader = new BufferedReader(new FileReader(path)); while ((strCurrentLine = objReader.readLine()) != null) { cont= cont + "--" + strCurrentLine; } objReader.close(); return cont; } } </cfjava> <cfset classInstance.init("content from cf")> <!--- calling constructor using init()---> <cfset path = ExpandPath('./') & 'temp.txt'> <cfset classInstance.write(path)> <!--- Calling the method write() of Class Harmless ---> <cfoutput>#classInstance.read(path)#</cfoutput> <!--- Calling the method read() of Class Harmless --->
Example- Script
<cfscript> classInstance = java{ public class class1{ public int execute () { int[] arr; arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; return arr.length; } } } writeoutput(classInstance.execute()) </cfscript>
Output
5