Perulo
Perulo

Reputation: 69

"getAppletContext().showDocument(new URL" not redirecting

I've integrated the following code in my main code:

import java.applet.*;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;

public class AppletExample extends Applet {

    public void init() {

       try {
             getAppletContext().showDocument(new URL("http://www.google.com"), "_blank");
       }
       catch (MalformedURLException ex) {
            System.out.println(ex.getMessage());
       }
    }

    public void paint( Graphics g ) {

       g.drawString("Go Google", 0,100);
    }

}

The idea is that getAppletContext().showDocument(new URL("http://www.google.com"), "_blank"); would redirect the user in applet, but it just won't do it. I've tried different stuff besides _blank, such as _self

What to do? Why doesn't it redirect?

Upvotes: 0

Views: 5042

Answers (1)

Lukas Knuth
Lukas Knuth

Reputation: 25755

There are multiple cases why this can happen:

First, to quote from the AppletContext-JavaDoc:

void showDocument(URL url)

Requests that the browser or applet viewer show the Web page indicated by the url argument. The browser or applet viewer determines which window or frame to display the Web page. This method may be ignored by applet contexts that are not browsers.

and to quote from your particular method-call (which is the overloaded version):

void showDocument(URL url, String target)

[...] An applet viewer or browser is free to ignore showDocument.

So, if you're not viewing the Applet in a browser or the browser does decide to ignore your call, you can't do anything about it.


Also, it seems that it depends on the VM-implementation if this is even supported in the first place. See this older post from an Apple Mailing-List.

Last but not least, when I tried it myself, it worked, but the request was blocked by the PopUp-Blocker (and had to be manually granted). I used:

  • Mozilla Firefox 12.0
  • Java(TM) SE Runtime Environment (build 1.7.0_04-b20), 64Bit
  • All on Linux

Upvotes: 1

Related Questions