ADF Code Corner. 71. How-to integrate Java Applets with Oracle ADF Faces. Abstract: twitter.com/adfcodecorner

Similar documents
ADF Code Corner Implementing auto suggest functionality in ADF Faces. Abstract:

ADF Code Corner How-to declaratively build a master-detail behavior with DVT components. Abstract: twitter.com/adfcodecorner

ADF Code Corner How-to bind custom declarative components to ADF. Abstract: twitter.com/adfcodecorner

ADF Code Corner How-to enforce LOV Query Filtering. Abstract: twitter.com/adfcodecorner

ADF Code Corner. 016-How-to customize the ADF Faces Table Filter. Abstract: twitter.com/adfcodecorner

ADF Code Corner How-to build a reusable toolbar with Oracle ADF Declarative Components. Abstract: twitter.com/adfcodecorner

ADF Code Corner. 65. Active Data Service Sample Twitter Client. Abstract: twitter.com/adfcodecorner

ADF Code Corner. Oracle JDeveloper OTN Harvest 01 / Abstract: twitter.com/adfcodecorner

ADF Code Corner. 97. How-to defer train-stop navigation for custom form validation or other developer interaction. Abstract: twitter.

ADF Mobile Code Corner

ADF Code Corner. 90. Filtering ADF bound lists. Abstract: twitter.com/adfcodecorner

ADF Code Corner How to cancel an edit form, undoing changes with ADFm savepoints

ADF Code Corner How-to use the af:autosuggestbehavior component tag with ADF bound data sources. Abstract: twitter.

ADF Code Corner. 70. How-to build dependent list boxes with Web Services Business Services. Abstract: twitter.com/adfcodecorner

ADF Code Corner. Oracle JDeveloper OTN Harvest 02 / Abstract: twitter.com/adfcodecorner

ADF Mobile Code Corner

ADF Code Corner How-to show a glasspane and splash screen for long running queries. Abstract: twitter.com/adfcodecorner

ADF Code Corner How-to launch a popup upon rendering of a page fragment in a region using JSF 2. Abstract: twitter.

ADF Mobile Code Corner

ADF Code Corner. 048-How-to build XML Menu Model based site menus and how to protect them with ADF Security and JAAS. Abstract:

ADF Code Corner. 64. How-to implement a Select Many Shuttle with pre- selected values. Abstract: twitter.com/adfcodecorner

ADF Mobile Code Corner

ADF Code Corner How-to restrict the list of values retrieved by a model driven LOV. Abstract: twitter.com/adfcodecorner

ADF Code Corner How-to further filter detail queries based on a condition in the parent view using ADF BC. Abstract: twitter.

ADF Code Corner How-to use Captcha with ADF Faces and Oracle ADF. Abstract: twitter.com/adfcodecorner

Using JavaScript in ADF Faces Rich Client Applications

ADF Region Interaction: External Train Navigation

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

ADF Code Corner. Oracle JDeveloper OTN Harvest 09 / Abstract: twitter.com/adfcodecorner

ADF Code Corner. Oracle JDeveloper OTN Harvest 10 / Abstract: twitter.com/adfcodecorner

<Insert Picture Here> Advanced ADF Faces. Frank Nimphius Principal Product Manager

Distributed Multitiered Application

ADF OAF Who Cares? You Do! Oracle Applications Framework / Application Development Framework - Which way do I go?

For live Java EE training, please see training courses at

Setup and Getting Startedt Customized Java EE Training:

Netscape Introduction to the JavaScript Language

ADF Code Corner. Oracle JDeveloper OTN Harvest 12 / Abstract: twitter.com/adfcodecorner

Vision of J2EE. Why J2EE? Need for. J2EE Suite. J2EE Based Distributed Application Architecture Overview. Umair Javed 1

User Scripting April 14, 2018

Developing Applications with Java EE 6 on WebLogic Server 12c

IBM. IBM WebSphere Application Server Migration Toolkit. WebSphere Application Server. Version 9.0 Release

Functional. Pattern Embeding Regions inside PopUp Windows

Java with Eclipse: Setup & Getting Started

CHAPTER. Introduction to the Oracle Application Development Framework

Enterprise Java Unit 1-Chapter 2 Prof. Sujata Rizal Java EE 6 Architecture, Server and Containers

Oracle Developer Day

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Java EE 6: Develop Web Applications with JSF

Take Your Oracle Forms on the Road Using ADF Mobile. Mia Urman, OraPlayer & Denis Tyrell, Oracle Corporation

SE - Deployment to Installed Polarion. SE - Execution from Workspace. SE - Configuration.

ADF Hands-On. Understanding Task Flow Activities / 2011 ADF Internal Enterprise 2.0 Training. Abstract:

Oracle Utilities Customer Self Service

ADF Code Corner. Oracle JDeveloper OTN Harvest 04 / Abstract: twitter.com/adfcodecorner

1Z0-430

1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Oracle Fusion Middleware 11g: Build Applications with ADF I

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

Oracle Fusion Middleware 11g: Build Applications with ADF I

Manually Run Java Update Windows 7 Registry

Mastering Oracle ADF Task Flows. Frank Nimphius Principal Product Manager Oracle JDeveloper / ADF

Oracle Developer Day

Oracle Fusion Middleware 11g: Build Applications with ADF Accel

If you wish to make an improved product, you must already be engaged in making an inferior one.

CSC 551: Web Programming. Fall 2001

CS506 Web Design & Development Final Term Solved MCQs with Reference

form layout - we will demonstrate how to add your own custom form extensions in to form layout

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Customizing Oracle Identity Governance: Populating Request Attributes

OES Permission Checks in ADF Task Flows

Oracle Application Development Framework Overview

Oracle Developer Day

Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat

Dynamic code downloading using Java TM (Using the java.rmi.server.codebase Property)

Guide to add as trusted site in Java 8 Update 51. Version of 24 OCBC Bank. All Rights Reserved

JBoss SOAP Web Services User Guide. Version: M5

How to Reference External JAR Files in Web Dynpro DC in SAP NW Portal 7.3

Mascot Insight Installation and setup guide

Page 1. Oracle9i OLAP. Agenda. Mary Rehus Sales Consultant Patrick Larkin Vice President, Oracle Consulting. Oracle Corporation. Business Intelligence

X100 ARCHITECTURE REFERENCES:

Developing Cross Device Mobile Applications

Getting Started with Eclipse/Java

Oracle Retail Accelerators for WebLogic Server 11g

Overview. Principal Product Manager Oracle JDeveloper & Oracle ADF

CS 209 Programming in Java #12 JAR Files: Creation and Use

Crystal Reports XI Release 2

Continuous Integration (CI) with Jenkins

Oracle Utilities Customer Self Service

Sentences Installation Guide. Sentences Version 4.0

Composer Deployment Guide. Installation

Oracle Developer Day

Creating your first JavaServer Faces Web application

Installing ArcIMS 9.1 on Windows

Java Error Applet Tag Missing Code Parameter Jnlp

Lecture Static Methods and Variables. Static Methods

202 Index. Data dictionary, 25 Data items defined, 25 overview, 26 Data Language/I (DL/I), 7 Data parts, 25 Data tables compared to dictionaries, 33

Agenda. Announcements. Extreme Java G Session 2 - Main Theme Java Tools and Software Engineering Techniques

J2EE Interview Questions

Rich Web UI made simple Building Data Dashboards without Code

Mobilize Your Users Now with Oracle Mobile Application Framework (MAF)

MAKING THE BUSINESS CASE MOVING ORACLE FORMS TO THE WEB

Transcription:

ADF Code Corner 71. How-to integrate Java Applets with Oracle ADF Faces Abstract: Oracle ADF Faces contains a JavaScript client framework that developers can use to integrate 3 rd party technologies like Java Applets and jquery. This article explains how to establish bi-directional communication between a Java Applet and an ADF Faces application. twitter.com/adfcodecorner Author: Frank Nimphius, Oracle Corporation twitter.com/fnimphiu 08-FEB-2011

Oracle ADF Code Corner is a loose blog-style series of how-to documents that provide solutions to real world coding problems. Disclaimer: All samples are provided as is with no guarantee for future upgrades or error correction. No support can be given through Oracle customer support. Please post questions or report problems related to the samples in this series on the OTN forum for Oracle JDeveloper: http://forums.oracle.com/forums/forum.jspa?forumid=83 Introduction ADF Faces provides developers with a comprehensive list of JavaServer Faces user interface components and behavior tags for building visually attractive web applications. Ideally, to ensure a consistent request lifecycle, developers build web applications with ADF Faces and / or 3 rd party JavaServer Faces components alone. However, there may be a need for ADF Faces to integrate with 3 rd party technology like Java Applet, JQuery or Flash, when the business case requires it. Integration with 3 rd party technology is on the client side with JavaScript, which also means that the application lifecylce and transaction is not shared. In this how-to article, I explain the integration of Oracle ADF Faces with Java Applets for bi-directional communication, as well as the Java Applet calling a managed bean. In the example, the Java Applet is added to the ADF Faces page as shown below: <APPLET code="adf.sample.applet.messageapplet.class" archive="/adfapplettalk-applettalk-context-root/messageapplet.jar" height="130" width="400" align="bottom"> This browser does not support Applets. </APPLET> Note that the APPLET tag is used instead of the EMBED or OBJECT tag that would be needed to explicitly invoke Java run by the Java PlugIn. As shown later, browsers can be configured to use the Java PlugIn instead of its own integrated Java Runtime Rnvironment (JRE), which in many cases does not exist, or is not sufficient to run latest Java. When installing a client side Java Runtime Environment the browser configuration is added automatically, so that using the APPLET tag is an option okay to use. 2

Client side only communication In the example, a Java Applet input text field and a command button are used to send text messages to the ADF Faces input form. The Java Applet uses the netscape.javascript.jsobject class contained in jre6\lib\plugin.jar to issue JavaScript calls to the containing page. private void sendmessagebutton_keypressed(keyevent e) { int keycode = e.getkeycode(); if (keycode == 10){ String message = messagefield.gettext(); JSObject win = (JSObject) JSObject.getWindow(this); //call the JavaScript messae if(printtoserver.isselected()){ //make sure message is printed to JSF managed bean win.eval("calladffromjavamessage('"+message+"',true);"); else{ win.eval("calladffromjavamessage('"+message+"',false);"); On the ADF Faces page, the JavaScript function calladffromjavamessage is defined as shown below: 3

function calladffromjavamessage(messagefromapplet, printtoserver){ var textfield = AdfPage.PAGE.findComponentByAbsoluteId("it1"); textfield.setvalue(messagefromapplet); textfield.focus(); if(printtoserver == true){ //send message as immediate AdfCustomEvent.queue(textField,"AppletIsCallingADF", {appletmessage:messagefromapplet,true); The JavaScript function uses the AdfPage.PAGE API to lookup the ADF Faces input text field component. It then determines the value of the printtoserver argument to determine whether to send the message to the server too. Note: When used in production, all of the JavaScript sources used in this sample should be stored in an external JavaScript library for better performance and code organization. When a message is sent from the Java Applet, it is printed to the ADF Faces input text field as shown here: Note that the message is added to the input text field such that submitting the ADF Faces form also submits the message to the Java Server Faces model or ADF binding. Calling a managed bean from a Java 4

Applet directly therefore is only required for events that should not update input fields on an ADF Faces page but only notify the server. Public methods of the Java Applet are accessible from JavaScript. In the image below, the ADF Faces form sends a message back to the Java Applet. The Applet displays the message and paints the text field background in yellow. 5

The public Java Applet method called from JavaScript is shown here: public void handlejavascripttoappletmessage(string message){ messagefield.setbackground(color.yellow); messagefield.settext(message); The JavaScript function to call into the Java Applet is invoked from an af:clientlistener tage on the Send Message command button. //JavaScript function calling client side function writeadffacesmessagetoapplet(evt) { var textfield = AdfPage.PAGE.findComponentByAbsoluteId("it1"); var message = textfield.getsubmittedvalue(); //talk to Applet document.applets[0].handlejavascripttoappletmessage(message); The Java Applet is accessed by its index in the browser page's document JavaScript reference. The JavaScript code reads the current value of the ADF Faces input text field, again using the AdfPage.PAGE API to look it up. Note ADF Faces components must have their clientcomponent property set to true or an af:clientlistener attached to be accessible from JavaScript. The latter is used in this example. Java Applet to managed bean communication A custom event is used in ADF Faces to call a managed bean from a Java Applet. The Java Applet calls a JavaScript function as before, but passes true as the value of the printtoserver argument. The following line of JavaScript queues the incoming Java Applet message as a custom ADF Faces event for a managed bean method to handle: AdfCustomEvent.queue(textField,"AppletIsCallingADF", {appletmessage:messagefromapplet,true); The textfield reference in the above code references the ADF Faces component that has the af:serverlistener tag defined to call the managed bean method. The name of the af.serverlistener tag on the page is AppletIsCallingADF. The Java Applet message is passed as the payload, a set of key-value pairs, of the custom event. In the example, a single argument is passed with the name of appletmessage. 6

The last argument specifies how the queued event should be handled by the ADF Faces request lifecycle. In the above example, the argument is set to immediate so the server call can be made without client validation to be performed on the ADF Faces page. As shown in the image above, the Java Applet in this sample uses a checkbox component in Swing for the user to indicate the call to be sent to a managed bean on the server too. 7

The server side managed bean receives an event object of type ClientEvent that contains the message sent by the Java Applet. public void onappletcall(clientevent clientevent) { String message = (String)clientEvent.getParameters().get("appletMessage"); //just print it out System.out.println("MESSAGE FROM APPLET"); System.out.println("==================="); System.out.println(message); System.out.println("==================="); For this to work, the af:serverlistener tag is configured on the input text field as shown below <af:inputtext id="it1" rows="5" columns="50" > <af:serverlistener type="appletiscallingadf" method="#{appletcallcenter.onappletcall "/> <af:clientlistener method="clearinputtextfield" type="dblclick"/> </af:inputtext> Note: For JavaScript in ADF Faces to find the af:inputtext component instance, the text field must have an af:clientlistener attached or its clientcomponent attribute set to true. If, like in the above case, the text field also has an af:serverlistener attached, then it must also have an af:clientlistener defined 8

ADF Faces managed bean to Applet communication Using the Apache Trinidad ExtendedRenderKitService class, managed bean in ADF Faces can call client JavaScript, which then can call back to the JavaApplet. This way, messages can be sent from the server to the client side Applet. Note: When communicating from a managed bean to the Java Applet, keep browser and network latency in mind. Especially when sending multiple messages, then, unless you handle queueing of server side messages, messages may get lost if the browser cannot handle the frequency you sent them. The image below shows a message sent from a managed bean in response to the Java Applet calling the server. 9

//send feedback back to the Applet FacesContext fctx = FacesContext.getCurrentInstance(); ExtendedRenderKitService erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class); erks.addscript(fctx, "window.writeanymessagetoapplet('your wish has been granted!')"); Configuring the Java Applet for deployment Java Applets are deployed with the application but downloaded to the client at runtime. This means that the Java Applet source, usually a Java Archive (JAR) file needs to be located in the Web project's publichtml folder or a sub-folder of it. The JAR file is then referenced from the Applet archive tag archive="/adfapplettalk-applettalk-context-root/messageapplet.jar" In the sample above, the archive reference to the Applet JAR file located in the public-html folder starts from the Java EE context root. The Java EE context root is determined by the Web project's Java EE Application property. You access this property by double clicking the Web project node in Oracle JDeveloper. Ensuring the JavaPlugin is used The one thing JavaScript and Java Applets have in common is that browser support varies. For Java Applets, the solution providing a consistent runtime environment is the Java Plugin that can be used instead of the browser native Java VM. 10

The Java Plugin is installed to replace the browser Virtual Machine when installing the Java Runtime Environment (JRE) to a computer. To test this on Windows, select Start Control Panel and then click Programs Click the Java entry, as shown below, to access the configuration panel. Select the Java tab to ensure the Java version that is installed. This information becomes handy in case you experience a Java minor or major version mismatch, which may happen when an Applet is compiled with an old version of Java. 11

Click the Advanced tab and expand the Default Java for browsers entry to see a list of browsers and their use of the Java Plugin at runtime. Note: Alternatively you can change the JSF page's Java Applet tag reference to use the EMBED and/or OBJECTS tag to enforce the use of the Java Plugin. In this case, the lookup of the Applet changes and the plugin also needs to have its mayscript property set to allow the Applet to send JavaScript to the client. If your Java Applet requires access to the client computer, then, in addition to downloading the JAR file, the JAR file needs to be signed with a certificate that the user needs to accept. Signing JAR files is out of scope for this article, but there are plenty resources available on the Internet describing this process. The infamous "incompatible magic value" issue Not every error message makes sense, and some error messages cause more confusion than the help they are supposed to provide. The infamous "incompatible magic value" message has confused developers over many years. Note: I don't want to imagine the cost caused by this misleading message as developers spent their time researching the real issue of the failed Java Applet loading instead of doing their job. The error message itself is shown below: java.lang.classformaterror: Incompatible magic value 1013478509 in class file adf/sample/applet/messageapplet at java.lang.classloader.defineclass1(native Method) 12

When you see this error message then, most likely the Java archive could not be found, in which case the server replies with http-404 or similar. The server side http-404 error message is delivered in an HTML document that the Java Plugin eagerly attempts to treat like a JAR file. Confused about the mismatch, the Plugin shows this confusing message. However, now you know that when the magic number shows incompatible, you need to double check the JAR or codebase setting (if not using JAR files for the Applet download). Download You can download the Java Applet and ADF Faces sources from the ADF Code Corner website. Its sample 71 and provided as Oracle JDeveloper workspaces http://www.oracle.com/technetwork/developer-tools/adf/learnmore/index-101235.html RELATED DOCOMENTATION Using JavaScript in ADF Faces applications Whitepaper http://www.oracle.com/technetwork/developer-tools/jdev/1-2011-javascript-302460.pdf 13