473,503 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LiveConnect applet crashing after page reload, please help

DKM
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.

The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
IE crashes when one refreshes the page or leave the page. This happens
only after calling the Java method more than once. It does not crash if
the Java method is called just once and then the page is refreshed.

FireFox does not crash at all and no error whatsoever. However, it
takes a good minute or two to display the newly created element. And,
if during that time if one calls the Java method, it gives an error
and it never works afterwords. But, once the Java method returns after
the first time, it works robustly no matter how many times the Java
method is called or if the page is refreshed or closed.

Can any LiveConnect guru please take a look at this code and let me
know whats wrong with it.

Thank you very much in advance.

D.K. Mishra

======== Hello.class ========
import java.applet.Applet;
import java.awt.Graphics;
import netscape.javascript.*;

public class Hello extends Applet {

private JSObject win;
private JSObject doc;

public void init() {
}

public void start() {
win = JSObject.getWindow(this);
doc =(JSObject) win.getMember("document");
}

//A set of 2 overloaded helper methods to create object array to pass
// as the 2nd argument to doc.call(string,Object[]).
public Object[] objArr(JSObject jso) {
Object[] ret = {jso};
return ret;
}
public Object[] objArr(String str) {
Object[] ret = {str};
return ret;
}

//This cretes a filled HTML Tag like <p>Hello</p> or
//<i>world!</i>.
public JSObject createFilledTag(String strTag, String strText) {
JSObject fragDoc = (JSObject)
doc.call("createDocumentFragment",null);
JSObject tagEle = (JSObject)
doc.call("createElement",objArr(strTag));
JSObject tagTextEle =
(JSObject)doc.call("createTextNode",objArr(strText ));
tagEle.call("appendChild",objArr(tagTextEle));
fragDoc.call("appendChild",objArr(tagEle));
return fragDoc;
}

//This method is called from javascript. It inserts
//*** Hello World! ***" into the empty <p id="para"></p>
//element
public void insertText(String str) {
JSObject paraEle = (JSObject) doc.call("getElementById",
objArr(str));
JSObject tmpEle = createFilledTag("b","*** Hello World! ***");
paraEle.call("appendChild",objArr(tmpEle));
}
}
======== hello.htm ========
<html>
<head>
<title> New Document </title>
<script>
function addElement() {
app = document.getElementById("Hello");
app.insertText("para");
}
</script>
</head>
<body>
<input type="button" onclick="addElement()" value="Fill"/>
<p id="para"></p>
<applet id="Hello" code="Hello.class" width="1" height="1"
mayscript="true" scriptable="true">
</applet>
</body>
</html>

Jul 23 '05 #1
8 3361
On 12-6-2005 4:02, DKM wrote:
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.

The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
IE crashes when one refreshes the page or leave the page. This happens
only after calling the Java method more than once. It does not crash if
the Java method is called just once and then the page is refreshed.

FireFox does not crash at all and no error whatsoever. However, it
takes a good minute or two to display the newly created element. And,
if during that time if one calls the Java method, it gives an error
and it never works afterwords. But, once the Java method returns after
the first time, it works robustly no matter how many times the Java
method is called or if the page is refreshed or closed.

Can any LiveConnect guru please take a look at this code and let me
know whats wrong with it.

Thank you very much in advance.

D.K. Mishra

[snip]

Your applet not only crashes in my IE6, but also in FF1.0.4, on various
occasions: repeated pressing the Fill button, leaving page and going
back (using browser's Forw & Back buttons).

I thought that it could be caused by the applet holding on to the win
and doc objects, but inlining the code of start() in the insertText and
createFilledTag methods didn't make any difference.

I guess that LiveConnect isn't as stable as it should be, unfortunately.
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
Jul 23 '05 #2
DKM


Roland wrote:
On 12-6-2005 4:02, DKM wrote:
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.

The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
IE crashes when one refreshes the page or leave the page. This happens
only after calling the Java method more than once. It does not crash if
the Java method is called just once and then the page is refreshed.

FireFox does not crash at all and no error whatsoever. However, it
takes a good minute or two to display the newly created element. And,
if during that time if one calls the Java method, it gives an error
and it never works afterwords. But, once the Java method returns after
the first time, it works robustly no matter how many times the Java
method is called or if the page is refreshed or closed.

Can any LiveConnect guru please take a look at this code and let me
know whats wrong with it.

Thank you very much in advance.

D.K. Mishra
[snip]

Your applet not only crashes in my IE6, but also in FF1.0.4, on various
occasions: repeated pressing the Fill button, leaving page and going
back (using browser's Forw & Back buttons).


In FireFox, if you don't click 'Fill' till it displays "Hello World!"
for the first time, it does not crash. Does it display "Hello World!"
immidiately when you click Fill for the first time? In my case, it
takes a good minute or two. After the first time you can click Fill as
many times as you want and it does not crash.

IE 6.0 displays "Hello World!" rightaway and keeps working till you
reload the page when it will crash.

I am still experimenting and will let you know if I succeed.

Thank you for your time and help. I really appreciate all the help and
tips. I am completely new to Java or any modern day COM programming.
Given that all these technology are not new and have been around for a
good decade, I thought there would be nice easy tools to design applets
that can interact with the browser's DOM.

Thank you again.

D.K. Mishra


I thought that it could be caused by the applet holding on to the win
and doc objects, but inlining the code of start() in the insertText and
createFilledTag methods didn't make any difference.

I guess that LiveConnect isn't as stable as it should be, unfortunately.
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \


Jul 23 '05 #3
I have found inconsistent actions regarding the calling of start() and
init() when a page containing an applet is reloaded. I used a kludge
where I always dumped all my data and started completely clean when
either were called.

Jul 23 '05 #4
On 11 Jun 2005 19:02:19 -0700, DKM wrote:
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.
Two of the most important aspects of a correctly functioning
applet driven DHTML web-page are..

- The HTML itself. If the HTML is not structurally
correct, a browser is free to interpret it any way
it likes/is best able[1]. You can validate your
HTML here <http://validator.w3.org/>..

- The Javascript that adds the 'D' to the Dynamic HTML.
It needs to be as robust as practicable, and that usually
depends on a technique known as 'feature detection'.
More details in the JS shown here..
<http://www.jibbering.com/faq/#FAQ4_15>
<title> New Document </title>
<script>
[1] This is not valid HTML.
function addElement() {
app = document.getElementById("Hello");
app.insertText("para");
}
</script>


[2] ..and that not robust script.

I suggest you strip all the Java out of it for the moment and
concentrate on getting the HTML and Javascript into a valid,
reliable form.

I have set the follow-ups to c.l.js only, though you may need
to consult a group such as c.i.w.a.h.[3] for HTML advice.

[3] <http://groups.google.com.au/group/comp.infosystems.www.authoring.html>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Jul 23 '05 #5
DKM


Andrew Thompson wrote:
On 11 Jun 2005 19:02:19 -0700, DKM wrote:
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.
Two of the most important aspects of a correctly functioning
applet driven DHTML web-page are..

- The HTML itself. If the HTML is not structurally
correct, a browser is free to interpret it any way
it likes/is best able[1]. You can validate your
HTML here <http://validator.w3.org/>..

- The Javascript that adds the 'D' to the Dynamic HTML.
It needs to be as robust as practicable, and that usually
depends on a technique known as 'feature detection'.
More details in the JS shown here..
<http://www.jibbering.com/faq/#FAQ4_15>
<title> New Document </title>
<script>


[1] This is not valid HTML.


I am totally at a loss. The whole thing is working if I use plain
javascript to add dyanmic stuff by calling createElement, appendChild
and such.

Now, all I am trying to do is create the same from inside an applet.
And, it works both in IE and Firefox, but as I pointed out IE crashes
when the page is refreshed and FireFox will crash only on one condition
and it can be prevented from crashing by setting a variable to false
before calling the method and wait till it gets set to true from inside
the applet.
function addElement() {
app = document.getElementById("Hello");
app.insertText("para");
}
</script>
[2] ..and that not robust script.

I suggest you strip all the Java out of it for the moment and
concentrate on getting the HTML and Javascript into a valid,
reliable form.


I have done this. I started without Java stuff and have it working in
both IE and FireFox.

I have now found a solution for the problem for IE. That is I am no
more creating dynamic elements inside the element. Instead, I am
creating all the dynamic stuff in a string and passing the string to
Javascript variable. Then from Javascript, I basically set the string
to the innerHTML of a container tag. It works perfectly. Now, ofcourse
the same does not work in FireFox. Its a XHTML file and the synamic
stuff contains tags. FireFox displays the < as &lt; and > as &gt; and
the content is displayed as is. For example it displays as
&lt;b&gt;"Hello World!!!&lt;\b&gt; instead of displaying Hello World!!!
in bold letters. I don't know how that can be taken cre of.

Again, thanks very much in advance.

D.K. Mishra


I have set the follow-ups to c.l.js only, though you may need
to consult a group such as c.i.w.a.h.[3] for HTML advice.

[3] <http://groups.google.com.au/group/comp.infosystems.www.authoring.html>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane


Jul 23 '05 #6
DKM


ch*********@dailycrossword.com wrote:
I have found inconsistent actions regarding the calling of start() and
init() when a page containing an applet is reloaded. I used a kludge
where I always dumped all my data and started completely clean when
either were called.


I am not sure I understand what you are saying. I have nothing in
init(). But, I have just the following in strat():

win = (JSObject) JSObject.getWindow(this);

What am I supposed to be doing in init(), start() and destroy()?

Thanks very much in advance.

D.K.Mishra

Jul 23 '05 #7
DKM


DKM wrote:
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.

The thing works both in IE 6.0 and FireFox 1.4. but with some problems.
IE crashes when one refreshes the page or leave the page. This happens
only after calling the Java method more than once. It does not crash if
the Java method is called just once and then the page is refreshed.

FireFox does not crash at all and no error whatsoever. However, it
takes a good minute or two to display the newly created element. And,
if during that time if one calls the Java method, it gives an error
and it never works afterwords. But, once the Java method returns after
the first time, it works robustly no matter how many times the Java
method is called or if the page is refreshed or closed.

Can any LiveConnect guru please take a look at this code and let me
know whats wrong with it.

Thank you very much in advance.

D.K. Mishra

======== Hello.class ========
import java.applet.Applet;
import java.awt.Graphics;
import netscape.javascript.*;

public class Hello extends Applet {

private JSObject win;
private JSObject doc;

public void init() {
}

public void start() {
win = JSObject.getWindow(this);
doc =(JSObject) win.getMember("document");
}

//A set of 2 overloaded helper methods to create object array to pass
// as the 2nd argument to doc.call(string,Object[]).
public Object[] objArr(JSObject jso) {
Object[] ret = {jso};
return ret;
}
public Object[] objArr(String str) {
Object[] ret = {str};
return ret;
}

//This cretes a filled HTML Tag like <p>Hello</p> or
//<i>world!</i>.
public JSObject createFilledTag(String strTag, String strText) {
JSObject fragDoc = (JSObject)
doc.call("createDocumentFragment",null);
JSObject tagEle = (JSObject)
doc.call("createElement",objArr(strTag));
JSObject tagTextEle =
(JSObject)doc.call("createTextNode",objArr(strText ));
tagEle.call("appendChild",objArr(tagTextEle));
fragDoc.call("appendChild",objArr(tagEle));
return fragDoc;
}

//This method is called from javascript. It inserts
//*** Hello World! ***" into the empty <p id="para"></p>
//element
public void insertText(String str) {
JSObject paraEle = (JSObject) doc.call("getElementById",
objArr(str));
JSObject tmpEle = createFilledTag("b","*** Hello World! ***");
paraEle.call("appendChild",objArr(tmpEle));
}
}
======== hello.htm ========
<html>
<head>
<title> New Document </title>
<script>
function addElement() {
app = document.getElementById("Hello");
app.insertText("para");
}
</script>
</head>
<body>
<input type="button" onclick="addElement()" value="Fill"/>
<p id="para"></p>
<applet id="Hello" code="Hello.class" width="1" height="1"
mayscript="true" scriptable="true">
</applet>
</body>
</html>

I posted the above code at Sun's bug database and I have received a
note that it is indeed a bug and they will try to fix it.

Here is the link to the bug database site at Sun's web site:

http://bugs.sun.com/bugdatabase/view...bug_id=6289379

Thanks everyone.

D.K. Mishra

Jul 23 '05 #8
On 11 Jun 2005 19:02:19 -0700, DKM wrote:

[ Note: Follow-ups set to comp.lang.java.programmer ]
The thing works both in IE 6.0 and FireFox 1.4. but with some problems.


I have a stable variant that I tried to add to add
your bug report at Sun..
<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6289379>

Most of text disappeared, so I will try posting it here..

.....
Try this as a potential 'workaround'.

I put '' because it takes a significantly
different approach - performing the bulk
of the element manipultion using JS functions
specially developed for X-browser compatibility.

This example fails when the user specifies
an element id that does not exist.

Developing a more robust script to account
for those sort of errors is left as an
exercise for the reader.

See the sources for further details and notes.

<sscce>
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;

public class Hello extends Applet implements ActionListener {

private JSObject win;

TextField element;
TextField text;

public void init() {
setLayout( new GridLayout(0,1) );
add(new Label("Element"));
element = new TextField("para");
add( element );

add(new Label("Text"));
text = new TextField(", some <em>new</em> text");
add( text );

Button btn = new Button("Add Text");
btn.addActionListener(this);
add( btn );
}

public void actionPerformed(ActionEvent ae) {
setPara();
}

public void start() {
win = JSObject.getWindow(this);
}

/** Calls a JS function that changes named
elements in the current page. */
public void setPara() {
try {
// all sorts of things can go wrong here..
win.eval( "setPara('" + element.getText() +
"', '" + text.getText() + "')" );
} catch (Throwable t) {
// ..let's find out what.
System.out.println( "There was a problem, " +
"see stacktrace for further details" );
t.printStackTrace();
}
}
}
</sscce>

---- index.html ----
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
It is better to specify HTML 4.01 (Strict), but this page includes the
<applet> element, which is deprecated in HTML 4.
-->
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=iso-8859-1">
<!--
From a bug report at Sun - Bug Id.: 6289379
http://bugs.sun.com/bugdatabase/view...bug_id=6289379
LiveConnect Applet crashes on page reload
from a thread on comp.lang.java.help,
Msg Id.: 11**********************@z14g2000cwz.googlegroups. com
http://groups-beta.google.com/group/...ddca8a2249914a

This version incorporates the getElementWithId(id) function shown here..
http://www.jibbering.com/faq/faq_not...ite.html#getEl
to deal with browser compatibility issues.
-->
<title>Document manipulation with Javascript/LiveConnect</title>
<script type='text/javascript' SRC='script.js'>
</script>
</head>
<!-- Calls the JS function to locate the applet only after the
onload* event has fired. * Indicating that the UA will now
have identified all the elements within the page.
-->
<body onload='getApp();'>
<div>
<input type="button" onclick="addElement()" value="Fill"/>
</div>
<!--
http://java.sun.com/products/plugin/.../jsobject.html
...lists the <applet> format of the 'mayscript' attribute as below.

The form used in the original page is refered to on that same
page as being appropriate for the <embed> structure only.
Neither the <embed> structure nor the MAYSCRIPT attribute
where ever part of any W3C recommendation.

Note that <applet> was part of HTML 3.2 but is deprecated in HTML 4.0.

I feel the <applet> element is too important and useful to lose
and should be *un*deprecated. But that is another matter.
-->
<applet id="Hello" code="Hello.class" width="100" height="100" MAYSCRIPT>
</applet>
<!-- These is our target elements, can they be altered reliably? -->
<p id="para">The &lt;P&gt; element (id="para"). Some text</p>
<div id="adiv">The &lt;DIV&gt; element (id="adiv"). Some text</div>

</body>
</html>
---- end index.html ----

---- script.js ----
var app;

/* Calls an x-browser script* developed for inclusion
in the comp.lang.javascript FAQ. * One of several
dealing with document manipulation. */
function getApp() {
app = getElementWithId("Hello");
}

/* Allows the HTML button to invoke the applet's 'setPara()
method. The applet's setPara() method, in turn, invokes the
(JS) setPara(id, txt) function using the current values of
the text fields in the applet. */
function addElement() {
app.setPara();
}

/* The method that changes the element.
Called by both the HTML button and the applet. */
function setPara(id, txt) {
document.getElementById(id).innerHTML =
document.getElementById(id).innerHTML + txt;
}

/* Script obtained from ..
http://www.jibbering.com/faq/faq_not...ite.html#getEl
See the URL for further discussion. */
function getElementWithId(id){
var obj = null;
if(document.getElementById){
/* Prefer the widely supported W3C DOM method, if
available:-
*/
obj = document.getElementById(id);
}else if(document.all){
/* Branch to use document.all on document.all only
browsers. Requires that IDs are unique to the page
and do not coincide with NAME attributes on other
elements:-
*/
obj = document.all[id];
}
/* If no appropriate element retrieval mechanism exists on
this browser this function always returns null:-
*/
return obj;
}
---- end script.js ----

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2447
by: WMMorgan | last post by:
There's a website I like to visit that has an user-interactive java application. There's a "visual applet" component and "control applet" component. (No, it's not an adult or porno site.) But...
4
15441
by: Warrick Wilson | last post by:
I've got a web page that uses frames. One of the frames loads an HTML page that redirects to a 3rd HTML page. This third page loads a Java applet - ProScroll.class - that runs a "news ticker"...
2
12769
by: Put 030516 in email subj to get thru | last post by:
I've always been bothered about having to statically declare the size of a Java applet window (container?) in the calling HTML. I've always wanted the moral equivalent of width=50% statement (of...
3
1513
by: Alan Pocklington | last post by:
Hi, I've created a Java Applet that allows the user to select a record. As the user makes the selection, the applet uses LiveConnect (JSObject) to write the record id to a html field (the name...
1
2062
by: Guillaume CABANAC | last post by:
Hi folks, Is liveconnect still available in Firefox as it is in NS Navigator ? I would like to call Java code from JavaScript in Firefox... Do you think it is possible with liveconnect ? Does...
1
1725
by: admin | last post by:
Hi all, I wrote a small text editor (using a JTextPane) in order to ease the use of a CMS, in a more WYSIWYG way. Basically, you can see directly the effect of setting the background color, the...
6
7649
by: j_macaroni | last post by:
I am trying to communicate back to my web page using LiveConnect. I just installed JDK5 Netbeans and have created a few applets. I have even called the applets from Javascript. However when I use...
1
2121
by: Alexandre Lahure | last post by:
Hi all, The facts : a rich text editing applet, a HTML/Javascript toolbar and Liveconnect to make them communicate alltogether. - Java to JS communication (for updating the state of the toolbar...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.