Calling an applet method from a Html file?
[nobr]Hi,
I'm working on some Sun API (XMLEncoder) to obtain a Swing applet, and add some code for understanding the XMLEncoder.
I need help to use the XMLEncoder Api with a Swing's applet. To resume, I would like to store an Object for long persistence storage...
But I make some kind mistake, while my XML output does'nt come out, I can't call my method applet getDocument() from my Html file! (See the thml source below,from the Get Html button..)
The calling method works fine when I call the method getDocument() direct from the applet, from the getDocument() applet button.
On some kind of condition, the code does'n not working for me; I find some problemes with the XMLEncoder API...
Remarque: When I try to encode an object like a string, the calling from the Html button works great, but when the object to being encode is an another type of object, like a Integer the writeObject() produce an error.
PS: Sorry and excuse me for my poor English ;-(
STRANGE BEHAVIOR: Call indirect from the GET Html Button button!
java.lang.NullPointerException
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Integer0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
</java>
WORKS GREAT: Call direct from the GET Applet button!
RESULT: <?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
<int>12</int>
</java>
Does someone see my mistake in my code?
And give me a suggestion how to fix it...
Thank
Stef
Applethtml.java
package applethtml;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.ByteArrayOutputStream;
import java.io.BufferedOutputStream;
publicclass Applethtmlextends Applet{
privateboolean isStandalone =false;
JPanel jPanel1 =new JPanel();
BorderLayout borderLayout1 =new BorderLayout();
JTextPane jTextPane1 =new JTextPane();
JButton jButton =new JButton();
public String getParameter(String key, String def){
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) !=null ? getParameter(key) : def);
}
public Applethtml(){
}
publicvoid init(){
try{
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
}
privatevoid jbInit()throws Exception{
jPanel1.setLayout(borderLayout1);
jTextPane1.setText("Exemple");
jButton.setText("Get");
jButton.addKeyListener(new Applethtml_jButton_keyAdapter(this));
this.addMouseListener(new Applethtml_this_mouseAdapter(this));
this.addMouseListener(new Applethtml_this_mouseAdapter(this));
this.add(jPanel1,null);
this.add(jTextPane1,null);
this.add(jButton,null);
}
publicvoid start(){
}
publicvoid stop(){
}
publicvoid destroy(){
}
public String getAppletInfo(){
return"Information applet";
}
public String[][] getParameterInfo(){
returnnull;
}
public String getDocument()
{
// Works great on all situation!
/* String obj = "test";
String enc = encode(obj);*/
String enc = encode(new Integer(12));
System.out.println(enc);
return enc;
}
private String encode(Object obj)
{
ByteArrayOutputStream baos =new ByteArrayOutputStream();
baos.reset();
java.beans.XMLEncoder xmlencoder =new java.beans.XMLEncoder(new BufferedOutputStream(baos));
xmlencoder.writeObject(obj);
xmlencoder.close();
return baos.toString();
}
String jButton_keyPressed(KeyEvent e){
String codeRetour = (String)getDocument();
System.out.println("RESULT: "+codeRetour);
return codeRetour;
}
String this_mouseClicked(MouseEvent e){
String codeRetour = (String)getDocument();
System.out.println("RESULT: "+codeRetour);
return codeRetour;
}
}
class Applethtml_jButton_keyAdapterextends java.awt.event.KeyAdapter{
Applethtml adaptee;
Applethtml_jButton_keyAdapter(Applethtml adaptee){
this.adaptee = adaptee;
}
publicvoid keyPressed(KeyEvent e){
adaptee.jButton_keyPressed(e);
}
}
class Applethtml_this_mouseAdapterextends java.awt.event.MouseAdapter{
Applethtml adaptee;
Applethtml_this_mouseAdapter(Applethtml adaptee){
this.adaptee = adaptee;
}
publicvoid mouseClicked(MouseEvent e){
adaptee.this_mouseClicked(e);
}
}
Applethtml.html
<html>
<head>
<title>
test_HTML
</title>
</head>
<body>
Exemple test<br>
<applet
codebase ="."
code="applethtml.Applethtml.class"
name="AppletTest"
width="400"
height="300"
hspace="0"
vspace="0"
align="middle"
>
</applet>
<BR>
<input type="button" value="get" onClick="alert(AppletTest.getDocument())">
</body>
</html>
[/nobr]
[9627 byte] By [
zebulaon] at [2007-9-30 11:24:12]

Javascript to java doesn't work when you use the applet tag. The applet tag should only be used for
ms jvm and is depreciated by the w3c for some time allready.
http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/js_java.html
Calling methods in a signed applet from javascript will not allowe the signed applet to do "restricted"
things because the whole stack should be given permission and the part that calls methods in your
applet from script (sun.plugin) is not "trusted" and will allways run with applet security restrictions.
http://forum.java.sun.com/thread.jsp?forum=63&thread=524815
second post
Hi, harmmeijer Thank for your response and the links, but I don't understand why the calling method works great if the object to encode is an object like the string: test? and doesn't work with an Integer...because the policy security are the same?
Sorry, javascript to java does work with the applet tag:
http://forum.java.sun.com/thread.jsp?forum=421&thread=533428
I never use the applet tag and when I did it didn't work for me but after some more testing it turns out it
does work.
http://forum.java.sun.com/thread.jsp?forum=421&thread=533428
Could you see the full trace when run with an int and run with a String?
To turn the full trace on you can start the java console, to be found here:
C:\Program Files\Java\j2re1.4...\bin\jpicpl32.exe
In the advanced tab you can fill in something for runtime parameters fill in this:
-Djavaplugin.trace=true -Djavaplugin.trace.option=basic|net|security|ext|liveconnect
The trace is here:
C:\Documents and Settings\your user\Application Data\Sun\Java\Deployment\log\plugin...log
[nobr]Checked out your code and doesn't look like a security or access exception.
Something goes wrong with writeObject(new Integer).
html file:
<html>
<head>
<title>
test_HTML
</title>
</head>
<body>
Exemple test<br>
<applet
codebase = "."
code= "Applethtml.class"
name= "AppletTest"
width= "400"
height= "300"
hspace= "0"
vspace= "0"
align= "middle"
>
</applet>
<BR>
<input type="button" value="String 22" onClick="alert(AppletTest.getDocument('22'))">
<input type="button" value="Integer 22 passed by script" onClick="alert(AppletTest.getDocument(22))">
<input type="button" value="Integer 22 hardcoded in applet" onClick="alert(AppletTest.getDocument())">
</body>
</html>
applet:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.ByteArrayOutputStream;
import java.io.BufferedOutputStream;
public class Applethtml extends Applet {
private boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JTextPane jTextPane1 = new JTextPane();
JButton jButton = new JButton();
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
public Applethtml() {
}
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(borderLayout1);
jTextPane1.setText("Exemple");
jButton.setText("Get");
jButton.addKeyListener(new Applethtml_jButton_keyAdapter(this));
this.addMouseListener(new Applethtml_this_mouseAdapter(this));
this.addMouseListener(new Applethtml_this_mouseAdapter(this));
this.add(jPanel1, null);
this.add(jTextPane1, null);
this.add(jButton, null);
}
public void start() {
}
public void stop() {
}
public void destroy() {
}
public String getAppletInfo() {
return "Information applet";
}
public String[][] getParameterInfo() {
return null;
}
public String getDocument(String obj)
{
String enc = encode((Object) obj);
System.out.println(enc);
return enc;
}
public String getDocument(Integer obj)
{
String enc = encode((Object) obj);
System.out.println(enc);
return enc;
}
public String getDocument()
{
Integer obj = new Integer(22);
System.out.println((Object) obj);
String enc = encode((Object) obj);
System.out.println(enc);
return enc;
}
private String encode(Object obj)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.reset();
java.beans.XMLEncoder xmlencoder = new java.beans.XMLEncoder(new BufferedOutputStream(baos));
xmlencoder.writeObject(obj);
xmlencoder.close();
return baos.toString();
}
String jButton_keyPressed(KeyEvent e) {
String codeRetour = (String)getDocument(new Integer(22));
System.out.println("RESULT: "+codeRetour);
return codeRetour;
}
String this_mouseClicked(MouseEvent e) {
String codeRetour = (String)getDocument(new Integer(33));
System.out.println("RESULT: "+codeRetour);
return codeRetour;
}
}
class Applethtml_jButton_keyAdapter extends java.awt.event.KeyAdapter {
Applethtml adaptee;
Applethtml_jButton_keyAdapter(Applethtml adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jButton_keyPressed(e);
}
}
class Applethtml_this_mouseAdapter extends java.awt.event.MouseAdapter {
Applethtml adaptee;
Applethtml_this_mouseAdapter(Applethtml adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.this_mouseClicked(e);
}
}
Trace:
Invoking method: public java.lang.String Applethtml.getDocument(java.lang.String)
Needs conversion: java.lang.String --> java.lang.String
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
<string>22</string>
</java>
Invoking method: public java.lang.String Applethtml.getDocument(java.lang.Integer)
Needs conversion: java.lang.Integer --> java.lang.Integer
java.lang.NullPointerException
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Integer0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
</java>
Invoking method: public java.lang.String Applethtml.getDocument()
22
java.lang.NullPointerException
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Integer0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
</java>
[/nobr]
This forum shows why (for example) File can't be written with XMLEncoder
http://forum.java.sun.com/thread.jsp?thread=262946&forum=62&message=1002333
(File is not serializable)
Integer has the same problem in my jvm version
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html
shows me that Integer does not have an empty constructor
to Solve this you cannot pass an Integer to the writeObject method, previously provided code should look
like this:
public String getDocument(Integer obj)
{
String enc = encode(obj.toString());
System.out.println(enc);
return enc;
}
public String getDocument()
{
Integer obj = new Integer(22);
System.out.println(obj.toString());
String enc = encode((Object) obj);
System.out.println(enc);
return enc;
}
Thank very much for the time you spent on my problem
If I realy understand the matter, it is while the constructor of the object to encode isn't empty?
But I doesn't understand why we can encode an object like an integer when we call the method direct from an applet button!
i.e:
String jButton_keyPressed(KeyEvent e) {
String codeRetour = (String) getDocument(new Integer(22));
System.out.println("RESULT: " + codeRetour);
return codeRetour;
}
an not from the html file:
<input type="button"
value="Integer 22 passed by script"
onClick="alert(AppletTest.getDocument(22))">
because the same method is been called?
i.e:
public String getDocument(Integer obj) {
String enc = encode( (Object) obj);
System.out.println(enc);
return enc;
}
[nobr]You are correct, same code started from javascript does behave differently. I set allpermission for all code
but still get some unexpected results from javascript. Don't know what causes it though.
Here is some code posted that would make it simpler to test this behavior:
applet:
import java.applet.*;
import java.io.ByteArrayOutputStream;
import java.io.BufferedOutputStream;
public class Applethtml extends Applet implements Runnable{
public Applethtml() {
}
public void init() {
}
public void start() {
new Thread(this).start();
}
public void run(){
System.out.println("this is run");
Thread.currentThread().dumpStack();
System.out.println(System.getProperty("java.version"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.reset();
java.beans.XMLEncoder xmlencoder = new java.beans.XMLEncoder(new BufferedOutputStream(baos));
xmlencoder.writeObject(new Integer(22));
xmlencoder.close();
System.out.println(baos.toString());
}
public void getDocument(){
System.out.println(System.getProperty("user.home")); // have set allpermission in
// C:\Program Files\Java\j2re1.4.2_03\lib\security\java.policy
// under grant {
// so the user.home is printed without any exception
System.out.println("this is from javascript"); // except for replacint the word run
// with from javascript in this line and this commend this code is exactly
// the same
Thread.currentThread().dumpStack();
System.out.println(System.getProperty("java.version"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.reset();
java.beans.XMLEncoder xmlencoder = new java.beans.XMLEncoder(new BufferedOutputStream(baos));
xmlencoder.writeObject(new Integer(22));
xmlencoder.close();
System.out.println(baos.toString());
new Thread(this).start();
}
public void stop() {
}
public void destroy() {
}
}
html
<html>
<head>
<title>
test_HTML
</title>
</head>
<body>
Exemple test<br>
<applet
codebase = "."
code= "Applethtml.class"
name= "AppletTest"
width= "400"
height= "300"
hspace= "0"
vspace= "0"
align= "middle"
>
</applet>
<BR>
<input type="button" value="String 22" onClick="AppletTest.getDocument();alert('done');">
</body>
</html>
I have no idea why the 2 methods would behave different but they do. Hope someone knows why because
I don't[/nobr]
I think that this must be some bug.
with Mozilla using 1.4.2_04 there is no problem:
This is the mozilla trace:
this is run
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at Applethtml.run(Applethtml.java:16)
at java.lang.Thread.run(Unknown Source)
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_04" class="java.beans.XMLDecoder">
<int>22</int>
</java>
my home dir
this is from javascript
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at Applethtml.getDocument(Applethtml.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin.liveconnect.PrivilegedCallMethodAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.liveconnect.SecureInvocation$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation.access$300(Unknown Source)
at sun.plugin.liveconnect.SecureInvocation$CallMethodThread.run(Unknown Source)
1.4.2_04
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_04" class="java.beans.XMLDecoder">
<int>22</int>
</java>
1.4.2_04
this is run
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at Applethtml.run(Applethtml.java:16)
at java.lang.Thread.run(Unknown Source)
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_04" class="java.beans.XMLDecoder">
<int>22</int>
</java>
This is the IE trace:
this is run
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at Applethtml.run(Applethtml.java:16)
at java.lang.Thread.run(Unknown Source)
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_04" class="java.beans.XMLDecoder">
<int>22</int>
</java>
Invoking method: public void Applethtml.getDocument()
my home dir
this is from javascript
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at Applethtml.getDocument(Applethtml.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)
at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
at sun.plugin.com.DispatchImpl$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
1.4.2_04
java.lang.NullPointerException
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Integer0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_04" class="java.beans.XMLDecoder">
</java>
1.4.2_04
this is run
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
at Applethtml.run(Applethtml.java:16)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Integer0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_04" class="java.beans.XMLDecoder">
</java>
You are correct, I confirm I tried your code (on reply 4 of 8) and it works with netscape but not with IE!
Maybe we must sign the applet with IE? I would like to try with keytool but I made an mistake...If you can help me... on the procedure.
Thank a lot.
I have no idea why the 2 methods would behave different but they do. Hope someone knows why because I don't
Me to.
> Maybe we must sign the applet with IE?
No, I don't think so. In my examples I gave allpermission to all code. I think it's because Mozilla/Netscape
use different plugins than IE so the problem is in the plugin.
As for signing aplets:
http://forum.java.sun.com/thread.jsp?forum=63&thread=524815
second post