Modify HTML using applet (org.w3c.dom.* ?)

Discussion in 'Java' started by Beat Zahnd, Apr 13, 2005.

  1. Beat Zahnd

    Beat Zahnd Guest

    Hello,

    Is it possible to modify the content of a displayed html-page form an
    applet.

    The page is generated by a cgi script and displays some status
    informations. This works well for slow changing data if the page is
    reladed every 10 - 60s. But for fast changing data it is not a nice
    solution.

    The page allready has an applet which receives multicast datagrams and
    displays this data. Even nicer would be if the applet could also
    modify the HTML content (i.e changing the backround color of a table
    cell to red if something fails).

    I found that the org.w3c.dom classes might be the right thing but
    failled to find a working example. Is this the right way to go or is
    there an other solution.

    Regards, Beat
    Beat Zahnd, Apr 13, 2005
    #1
    1. Advertising

  2. Beat Zahnd

    John Guest

    Beat Zahnd wrote:
    > Hello,
    >
    > Is it possible to modify the content of a displayed html-page form an
    > applet.
    >
    > The page is generated by a cgi script and displays some status
    > informations. This works well for slow changing data if the page is
    > reladed every 10 - 60s. But for fast changing data it is not a nice
    > solution.
    >
    > The page allready has an applet which receives multicast datagrams and
    > displays this data. Even nicer would be if the applet could also
    > modify the HTML content (i.e changing the backround color of a table
    > cell to red if something fails).
    >
    > I found that the org.w3c.dom classes might be the right thing but
    > failled to find a working example. Is this the right way to go or is
    > there an other solution.
    >
    > Regards, Beat


    I would use a big applet to do everything in this scenario. If it got
    too complicated then I would look at a web start or standalone application.

    John
    John, Apr 13, 2005
    #2
    1. Advertising

  3. Beat Zahnd

    Roland Guest

    On 13-4-2005 10:50, Beat Zahnd wrote:
    > Hello,
    >
    > Is it possible to modify the content of a displayed html-page form an
    > applet.
    >
    > The page is generated by a cgi script and displays some status
    > informations. This works well for slow changing data if the page is
    > reladed every 10 - 60s. But for fast changing data it is not a nice
    > solution.
    >
    > The page allready has an applet which receives multicast datagrams and
    > displays this data. Even nicer would be if the applet could also
    > modify the HTML content (i.e changing the backround color of a table
    > cell to red if something fails).
    >
    > I found that the org.w3c.dom classes might be the right thing but
    > failled to find a working example. Is this the right way to go or is
    > there an other solution.
    >
    > Regards, Beat

    Yes, you can. Not using org.w3c.dom, but something called 'LiveConnect'.
    LiveConnect allows you to interact with the browser's DOM from within
    the applet. It's sort of using JavaScript from within Java.

    <http://wp.netscape.com/eng/mozilla/3.0/handbook/plugins/>

    The class below shows an applet setting and reading a cookie. Also
    included a HTML doc demonstrating the applet.

    Some points for using and compiling:
    1) To be able to use a 'LiveConnect' applet, the attribute "MAYSCRIPT"
    is required in the <applet> tag.
    2) To compile, you need to include 'plugin.jar' in the classpath; this
    contains the netscape.javascript.JSObject class. 'plugin.jar' can be
    found in the 'lib' directory of your JRE, e.g. C:\Program
    Files\Java\j2re1.4.2\lib\plugin.jar
    3) It's likely not to work with Microsoft's VM, but only with browsers
    with Sun's Java plugin. (tried it successfully with Mozilla and IE6 with
    the jre1.5 plugin)
    4) To see what the applet is printing, open the Java console.
    5) For a session cookie: if you don't specify an expires=<gmtDate> for
    the cookie, the browser will remove the cookie at end of session. If you
    want to set expiration, you'll need to enhance the applet's setCookie method

    ---------File: SGCookie.java --------------
    import java.applet.Applet;
    import netscape.javascript.JSException;
    import netscape.javascript.JSObject;
    /**
    * Demonstration of setting cookies from within an applet.
    * <p>
    * To be able to use this applet, the attribute "MAYSCRIPT" is required
    * in the applet tag, e.g.
    * <pre>
    * &lt;applet
    * code="SGCookie.class"
    * codebase="."
    * name="cookieApp"
    * MAYSCRIPT
    * &gt;
    * &lt;/applet&gt;
    * </pre>
    */
    public class SGCookie extends Applet
    {
    private JSObject window;
    private JSObject document;

    public void init()
    {
    // Get a reference to the DOM window and document objects
    try
    {
    window = JSObject.getWindow(this);

    System.out.println("init()\twindow=" + window);

    document = (JSObject) window.getMember("document");

    System.out.println("init()\tdocument=" + document);
    }
    catch (JSException ex)
    {
    // We couldn't get reference to either object
    ex.printStackTrace();
    }
    }

    public void setCookie(String name, String value)
    {
    if (document == null)
    {
    // init() failed to obtain a reference to document object.
    System.out.println("Can't set cookie: no document reference");
    return;
    }

    try
    {
    /*
    * should do here checks to ensure name and value don't contain
    * invalid characters and name is not an empty string
    * ...
    */

    String cookieText = name + "=" + value;

    System.out.println("Setting document.cookie = \""
    + cookieText + '"');

    // Set document member "cookie" to cookieText
    // Similar to Javascript document.cookie = "name=value";
    document.setMember("cookie", cookieText);

    // Read document cookies; This might return multiple
    // name=value pairs separated by ';' (a document can
    // have more than one cookie)
    // Similar to Javascript cookies = document.cookie;
    Object cookies = document.getMember("cookie");

    System.out.println("Got document.cookie = \"" + cookies + '"');
    }
    catch (JSException ex)
    {
    // Something went wrong
    ex.printStackTrace();
    }
    }
    }
    ---------------------------------------






    ---------File: demo.html --------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Set Cookie With Java Applet</title>
    </head>
    <body>
    <h1>Set Cookie With Java Applet</h1>
    <form name="myForm">
    Cookie name: <input Type="text" value="foo" name="cookieName">
    = value: <input Type="text" value="bar" name="cookieValue">
    <input Type="button" value="Set Cookie"
    onClick="document.cookieApp.setCookie(document.myForm.cookieName.value,
    document.myForm.cookieValue.value)">
    </form>
    <!-- The MAYSCRIPT attribute is required in order for the Java methods
    to be available to the JavaScript functions. -->
    <applet code="SGCookie.class" codebase="." name="cookieApp" MAYSCRIPT>
    </applet>
    </body>
    </html>
    ---------------------------------------


    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
    Roland, Apr 13, 2005
    #3
  4. Beat Zahnd

    Beat Zahnd Guest

    Roland <> wrote in message news:<425cf582$0$147$4all.nl>...
    > Yes, you can. Not using org.w3c.dom, but something called 'LiveConnect'.
    > LiveConnect allows you to interact with the browser's DOM from within
    > the applet. It's sort of using JavaScript from within Java.


    Hei, thanks very much. I got it working with this package on Mozilla
    1.7, IE6 and Safari 1.2 (all using Sun's Java plugin) without any
    problem.

    Regards, Beat
    Beat Zahnd, Apr 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    1
    Views:
    6,352
    Darren Davison
    Aug 5, 2003
  2. mynamehere
    Replies:
    0
    Views:
    451
    mynamehere
    Dec 14, 2003
  3. Praveen Chhangani

    Converting a org.jdom DOC to org.w3c DOC

    Praveen Chhangani, Aug 5, 2003, in forum: XML
    Replies:
    2
    Views:
    963
    Johannes Koch
    Aug 7, 2003
  4. Alan
    Replies:
    6
    Views:
    1,588
  5. Jozza
    Replies:
    1
    Views:
    1,841
    Jozza
    Mar 4, 2010
Loading...

Share This Page