Entries Tagged as Java

Comusming both CFMX and Java Web Services

July 23, 2004 · No Comments

Strangely enough, creating Java web services is just as easy as creating ColdFusion web services. And you can easily consume both. Here's a ridiculously easy and completely useless example of how to do it. First, our .cfc
<cfcomponent displayname="HelloWebService">
   <cffunction access="remote" output="No" name="getHello"
returntype="string">

   
      <cfreturn "Hello, I'm a ColdFusion Web Service" />
   </cffunction>
</cfcomponent>
Nothing there you haven't seen before. Now for our Java Web Service.
public class HelloWebService
{
   public String getHello()
   {
      return "Hello, I'm a Java Web Service";
   };
};
I think it's fairly obvious what's happening in the code above. Now to consume it all you need is something like so...
<ul>
   <li><a href="webservice.cfm?ws=CFML">CFML Hello webservice</a></li>
   <li><a href="webservice.cfm?ws=JAVA">JAVA Hello webservice</a></li>
</ul>

<cfif IsDefined("url.ws")>
   <cfswitch expression="#url.ws#">
   <cfcase value="cfml">
   <cfset objHello = createObject('webservice',
'http://dev.creative-restraint.co.uk/HelloWebService.cfc?WSDL')>

   <cfoutput>
   #objHello.getHello()#
   </cfoutput>
   </cfcase>
   <cfcase value="JAVA">
   <cfset objHello = createObject('webservice',
'http://dev.creative-restraint.co.uk/HelloWebService.jws?WSDL')>

   <cfoutput>
   #objHello.getHello()#
   </cfoutput>
   </cfcase>
   </cfswitch>
</cfif>
And there you go. A completely useless example but at least you know you can do it. Oh, I should point out that this works on CFMX Standard as well as Enterprise.

No Comments Tags: ColdFusion · Java

File Reading

July 21, 2004 · 2 Comment s

We alll know that we can use <CFFILE> to read in the contents of a file. However, what if you're on a shared hosting account and your host has disabled access to the <CFFILE> tag? You can use Java to get around this. Of course, the only issue you've got here is if your host has also disabled <CFOBJECT> and <createObject> - in which case, go get a new hosting account with someone better. You can use the following code (borrwed nicely from Aaron Johnson, with only a very minor change):
<cfscript>
fileAsString = "";
fileToRead = "d:/www/wwwroot/test/test/testFile.txt";
fileReader = createObject("java", "java.io.FileReader");
fileReader.init(fileToRead);
bufferedReader = createObject("java", "java.io.BufferedReader");
bufferedReader.init(fileReader);
try {
   do {
      fileAsString = bufferedReader.readLine();
      processLine(fileAsString);
   } while (true);
} catch (coldfusion.runtime.UndefinedVariableException e) {
// this indicates end of file, ok to ignore error
}
</cfscript>

<cffunction name="processLine">
   <cfargument name="line" required="true">
   <cfoutput>#arguments.line#<br /></cfoutput>
</cffunction>
In Aaron's example, the code was taking the file from a file upload, but here we're reading in from a file on disk. The key thing to remember is to provide the full path to the file. The other benefit you have with using this method to read in (and then output) the contents of the file is that it is done line by line, whereas <CFFILE> loads the entire file into memory at one time.

2 Comment s Tags: ColdFusion · Java