By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

LiveConnect applet crashing after page reload, please help

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.