{"id":303,"date":"2013-09-09T20:50:30","date_gmt":"2013-09-09T12:50:30","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=303"},"modified":"2018-09-04T13:04:13","modified_gmt":"2018-09-04T05:04:13","slug":"how-to-send-message-from-java-applet-to-web-page-via-jquery","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-send-message-from-java-applet-to-web-page-via-jquery\/","title":{"rendered":"How to send message from Java applet to web page via jQuery"},"content":{"rendered":"<p>In an attempt to save development time on reworking the RIAs from a old system, a business decision was made to reuse the Java applets on a newer system. <\/p>\n<p>The newer system utilizes jQuery at the front end to fulfill modern user interactivity requirements. <\/p>\n<p>This post documents a proof of concept that I did for the powerful Java applets to communicate with the front-end of the newer system.<\/p>\n<h2>The host, the middle man and the Java applet<\/h2>\n<p>I divided this proof of concept into three main parts:<\/p>\n<ol>\n<li><strong>The host<\/strong> which is the web page that contains the middle man and the Java applet.<\/li>\n<li><strong>The middle man<\/strong> which is a Javascript function that the Java applet will call to pass the message to other components on the web page. <\/li>\n<li><strong>The Java applet<\/strong> which will harvest the message and send it to the host.<\/li>\n<\/ol>\n<h2>The host and the middle man<\/h2>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN&quot; \r\n    &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Sending message from Java applet to web page via jQuery - Proof Of Concepts&lt;\/title&gt;\r\n    &lt;script src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.10.2\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;h4&gt;Message from applet:&lt;\/h4&gt;\r\n\t&lt;div id=&quot;containerToWriteTo&quot;&gt;\r\n\t\t\r\n\t&lt;\/div&gt;\r\n\t\r\n\t&lt;h4&gt;The applet&lt;\/h4&gt;\r\n\t&lt;applet name=&quot;MessageSendingApplet&quot; \r\n\t\t\tcode=&quot;com\/techcoil\/applet\/send\/message\/to\/webpage\/MessageSendingApplet.class&quot; \r\n\t\t\tarchive=&quot;http:\/\/www.techcoil.com\/resource\/java\/applet\/MessageSendingApplet.jar&quot; \r\n\t\t\theight=&quot;45&quot;\r\n\t\t\twidth=&quot;350&quot;&gt;\r\n\t&lt;p&gt;You have to activate Java in order to view this proof of concept&lt;\/p&gt;\r\n\t&lt;\/applet&gt;\r\n\t\r\n\t&lt;script type=&quot;text\/javascript&quot;&gt;\r\n\t\tfunction writeToContainer(msg) {\r\n\t\t\t$('#containerToWriteTo').html(msg);\r\n\t\t}\r\n\t&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>In the <code>body<\/code> tag, I define a <code>div<\/code> section to hold the message from the applet and identified it as <code>containerToWriteTo<\/code>. <\/p>\n<p>I then use the <code>applet<\/code> tag to hold the applet that I am going to create.<\/p>\n<p>Lastly, near the closing <code>body<\/code> tag, I define the <code>writeToContainer<\/code> Javascript function with an input parameter msg. In the function, I use <a href=\"http:\/\/jquery.com\/\" title=\"LInk to jQuery homepage\" target=\"_blank\">jQuery<\/a> to look for the <code>containerToWriteTo<\/code> <strong>div<\/strong> and set its content with whatever that is passed into the function via the <code>msg<\/code> parameter.<\/p>\n<h2>The Java applet<\/h2>\n<h3>Pre compilation work<\/h3>\n<p>The <a href=\"http:\/\/docs.oracle.com\/javafx\/2\/api\/netscape\/javascript\/JSObject.html\" title=\"JSObject java documentation\"><code>netscape.javascript.JSObject<\/code><\/a> class is needed for my Java applet to call some Javascript functions that is contained in the same webpage where it is embedded at. <\/p>\n<p>And for my Java applet to be compiled successfully while it uses <code>JSObject<\/code>, I will need to specify the <strong>plugin.jar<\/strong> library found in the \/lib folder in the build path.<\/p>\n<h3>The Java codes to realise the applet<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.techcoil.applet.send.message.to.webpage;\r\n\r\nimport java.awt.Dimension;\r\nimport java.awt.event.ActionEvent;\r\nimport java.awt.event.ActionListener;\r\n\r\nimport javax.swing.JApplet;\r\nimport javax.swing.JButton;\r\nimport javax.swing.JOptionPane;\r\nimport javax.swing.JPanel;\r\nimport javax.swing.JTextField;\r\nimport javax.swing.SwingUtilities;\r\n\r\nimport netscape.javascript.JSException;\r\nimport netscape.javascript.JSObject;\r\n\r\npublic class MessageSendingApplet extends JApplet {\r\n\r\n\tprivate static final long serialVersionUID = 1L;\r\n\t\r\n\tprivate JButton sendMessageButton;\r\n\tprivate JTextField messageTf;\r\n\tprivate JSObject browserWindow;\r\n\t\r\n\tpublic void init() {\r\n\t\tSwingUtilities.invokeLater(new Runnable() {\r\n\t\t    public void run() {\r\n\t\t    \tinitApplet();\r\n\t\t    }\r\n\t\t});\r\n\t}\r\n\t\r\n\tprivate void initApplet() {\r\n\t\t\r\n\t\tthis.setSize(new Dimension(350, 45));\r\n\t\t\t\r\n\t\tJPanel mainPane = new JPanel();\r\n\t\t\r\n\t\tActionListener submitMessageActionListener = new ActionListener() {\r\n\t\t\t\r\n\t\t\tpublic void actionPerformed (ActionEvent e) {\r\n\t\t\t\tsendMessageToWebpage();\r\n\t\t\t}\r\n\t\t\t\r\n\t\t};\r\n\t\t\r\n\t\tthis.messageTf = new JTextField();\r\n\t\tthis.messageTf.setPreferredSize(new Dimension(200, 30));\r\n\t\tthis.messageTf.setMinimumSize(new Dimension(80, 30));\r\n\t\tthis.messageTf.addActionListener(submitMessageActionListener);\r\n\t\t\r\n\t\tthis.sendMessageButton = new JButton(&quot;Send message&quot;);\r\n\t\tthis.sendMessageButton.setToolTipText(&quot;Send the message in the text field to the web page.&quot;);\r\n\t\tthis.sendMessageButton.setPreferredSize(new Dimension(120, 29));\r\n\t\tthis.sendMessageButton.addActionListener(submitMessageActionListener);\r\n\t\t\r\n\t\tmessageTf.requestFocus();\r\n\t\t\r\n\t\tmainPane.add(messageTf);\r\n\t\tmainPane.add(sendMessageButton);\r\n\t\t\r\n\t\tsetContentPane(mainPane);\r\n\t\t\r\n\t\ttry {\r\n\t\t\tbrowserWindow = JSObject.getWindow(this);\r\n\t\t} catch(JSException jse) {\r\n\t\t\tthis.displayErrorMessage(&quot;Unable to get a reference to the browser window.&quot;);\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate void sendMessageToWebpage() {\r\n\t\tif (this.browserWindow != null) {\r\n\t\t\ttry {\r\n\t\t\t\tbrowserWindow.eval(&quot;writeToContainer('&quot; + this.messageTf.getText() + &quot;')&quot;);\r\n\t\t\t}\r\n\t\t\tcatch (JSException jse) {\r\n\t\t\t\tthis.displayErrorMessage(jse.getMessage());\r\n\t\t\t} \/\/ end try-catch\r\n\t\t}\r\n\t\telse {\r\n\t\t\tthis.displayErrorMessage(&quot;Unable to get a reference to browser window.&quot;);\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate void displayErrorMessage(String msg) {\r\n\t\tJOptionPane.showMessageDialog(this, \r\n\t\t\t\tmsg, \r\n\t\t\t\t&quot;Message Sending Applet&quot;,\r\n\t\t\t\tJOptionPane.ERROR_MESSAGE);\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<h3>Building the applet user interface<\/h3>\n<p>I started building the code by extending a subclass from the <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/swing\/JApplet.html\" title=\"Java documentation for javax.swing.JApplet\"><code>javax.swing.JApplet<\/code><\/a> class. I gave life to the applet by overriding the init() function. I then use the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/SwingUtilities.html#invokeLater(java.lang.Runnable)\" title=\"Java documentation for SwingUtilities.invokeLater method\"><code>SwingUtilities.invokeLater<\/code><\/a> function to ensure that the applet was created by the AWT event dispatch thread.<\/p>\n<p>Within the <code>initApplet<\/code> method, I create the user interface components for the applet to interact with the user:<\/p>\n<ul>\n<li>A text field to collect messages from the user <\/li>\n<li>A button for the user to submit the messages<\/li>\n<li>A action handler that sends messages from the text field to the web page<\/li>\n<\/ul>\n<p>Lastly, I get a reference to the Javascript window object for the HTML document via the <code>JSObject.getObject<\/code> function and remembers it with the <code>browserWindow<\/code> variable.<\/p>\n<h3>Sending the message to the web page<\/h3>\n<p>When the user press enter or click on the \"Send message\" button, the <code>sendMessageToWebpage<\/code> method will be called. In the <code>sendMessageToWebpage<\/code> method, I use the <code>JSObject.eval<\/code> method to invoke the middle man function, passing it the text value that I get from the text field.<\/p>\n<div class=\"callToAction\">\n   <a href=\"http:\/\/www.techcoil.com\/proof-of-concepts\/send-message-from-java-applet-to-webpage\" class=\"readMoreLink\">See this proof of concept<\/a>\n<\/div>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-4T\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4T&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4T&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F303&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>In an attempt to save development time on reworking the RIAs from a old system, a business decision was made to reuse the Java applets on a newer system. <\/p>\n<p>The newer system utilizes jQuery at the front end to fulfill modern user interactivity requirements. <\/p>\n<p>This post documents a proof of concept that I did for the powerful Java applets to communicate with the front-end of the newer system.<\/p>\n","protected":false},"author":1,"featured_media":1220,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[16,137,6,162,128,89],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Java-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-4T","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/303"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1220"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}