• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Java and browser windows

Paul C. Anagnostopoulos

Nap, interrupted.
Joined
Aug 3, 2001
Messages
19,141
A Java applet normally doesn't have permission to open a new browser window (say, to display a page with help). I find this odd, but anyhoo. Does anyone know how to get around this restriction, short of granting full permissions to the applet?

~~ Paul
 
You want to read up on the security manager classes - the design is such so that you can restrict the things an applet can do with fine granularity - rather than just all or nothing.
 
Use javascript in tandem with java? Or have a much larger area applet with two virtual windows embedded inside it.
 
I'll read up on those, Cyborg.

I can use Javascript in tandem and it has permission by default? Then why doesn't Java?

~~ Paul
 
A Java applet normally doesn't have permission to open a new browser window (say, to display a page with help).
My brain is a tad slow this morning, and I very seldom touch applets, but I cannot recall having any problems opening browser windows. Can you give some specifics of the documented restrictions, or of what you're trying to do?
 
I'll read up on those, Cyborg.

You'll want to read up on jar file signing as well otherwise all will be for naught. Your applet will need to be trusted before security is reduced at all - and from my experience there are still limitations there. I never did get my applet to do what I wanted so I had to use a PHP layer as glue.

I can use Javascript in tandem and it has permission by default? Then why doesn't Java?

Just remember Javascript != Java. They're two different environments made by two different groups of people that only really share a namesake in common.

Java is generally considered more dangerous - hence the extra restrictions. It's a pain for programming sometimes but it's better than having rouge websites delete your harddrive.

Javascript - or ECMA script - is entirely within the environment of the browser and is hence sandboxed in that context. What actions might be restricted is determined by the browser - e.g. pop-up blocking. However unless there is some security flaw in the browser itself it is not generally possible for Javascript alone to harm your computer. Hence there are generally no restrictions.
 
Fungrim said:
My brain is a tad slow this morning, and I very seldom touch applets, but I cannot recall having any problems opening browser windows. Can you give some specifics of the documented restrictions, or of what you're trying to do?
I'm trying to implement help by the simple expedient of opening a browser window pointed at a page with the help information on it. This requires using the Runtime.exec method, which is restricted to standalone apps by default.

~~ Paul
 
I think it's a deliberate feature of applets that they can't open new browser windows. People were (rightly) worried that a program running on a client PC could potentially do damage. Because of this, applets are greatly restricted - for example, they can't write files to the local machine, or read data from there (and send it back to the server). Preventing applets from opening new windows is one of the restrictions designed to make people feel that java applets are safe enough to be allowed.

Java applications (i.e. not applets) are different - they can do most anything - the same as you could with C++ or any other language.

Javascript is unfortunately named and has nothing to do with java other than its confusing name. As it is a scripting engine for browsers, it is natural that it allows new windows to be opened, but this has been so abused (pop ups) that most PCs will nowadays have a pop-up stopper of some kind installed.

I would have the inital html launch one or more windows containing two applets - one for the helper and one for the main application. These could then communicate with each other, probably via the server.

Of course, once you have to do some server-side stuff to tie the two applets together, you start to wonder whether it's not easier to dispense with the applets, and do the whole thing on the server with php, or whatever. I suppose it depends how computationally intensive the task is, and how many users are likely to access it simultaneously.
 
Last edited:
I'm trying to implement help by the simple expedient of opening a browser window pointed at a page with the help information on it.
I'm still feeling off beat, do you mean than...
getAppletContext().showDocument(new URL("http://www.google.com"), "_blank");
... doesn't work, or isn't doing what you require?
 
I'm still feeling off beat, do you mean than...
getAppletContext().showDocument(new URL("http://www.google.com"), "_blank");
... doesn't work, or isn't doing what you require?

Inspired by this thread, I tried doing exactly that last night when I first read the OP, and it worked for me...
 
JamesM, show me your code so I will know which of the six trillion Java packages I need. Will this code work in an application, too?

I confess to all who are watching that I'm using a class given to me by long-time Java programmer. When I plopped it into my app I expressed surprise at myself, because I rarely take anyone else's code without rewriting it first. Serves me right.

~~ Paul
 
I see much gnashing of teeth about getAppletContext().showDocument() at the java.sun site.

I tried, but, as usual, I cannot determine which imports I need to compile that code.

~~ Paul
 
Ah! If that works, then sorry if my ramblings misled anyone. I've never tried that from an applet.
 
I'm not saying this is actually going to work, I'm just saying that when I tried it, it opened a new window on my browser, so it will at least establish whether you really do have a security problem.

Code:
import java.applet.Applet;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;

public class AppletMethod extends Applet {
    public void start() {
        try {
            getAppletContext().showDocument(
                    new URL("http://www.randi.org"), "_blank");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paint(Graphics g) {
        g.drawString("Behold the window-opening power of Java", 50, 50);
    }
}
the HTML file was just:

Code:
<applet code="AppletMethod.class" width="400" height="75">
    <param name="text" value="an applet">
    <hr/>
</applet>
 
I see much gnashing of teeth about getAppletContext().showDocument() at the java.sun site.
Yes, but most of it sems unrelated. Only had a quick look though. The Applet technology is a major failure in Java. When Java was released everyone thought Applets was the one thing that would propeller Java into the mainstream. In hindsight, I'm not very surprised it failed.

I tried, but, as usual, I cannot determine which imports I need to compile that code.
As said above, you need the java.applet package if you want to reference the Applet Context. Note: Don't use the '*' for inclusion of entire packages unless you're forced to code the import by hand. If you're using a decent editor it should be able to fix the import explicitly for you, which reduces namespace clash agonies.

And remember, the Java API documentation is your friend.
 
@Override
Please note that the "@" marks an annotation and is Java 5.0 specific only. It will fail in older compilers. If you're compiling in < 5.0 just remove the "@Override" from the code. (And if you're a friend of readable code, do it anyway).
 
Will this code work in an application, too?
No. The applet context represents, surprise, the context in which the applet is run. As the docs says:

This interface corresponds to an applet's environment: the document containing the applet and the other applets in the same document.
It will not function outside a loaded applet (probably not in the applet viewer either) and the context will be invalidated as soon as the browser leaves page the applet is loaded on. So calling...

Code:
getAppletContext().showDocument(myUrl, "_self");
.. should work but will result in the applet being stopped and the applet context invalidated.
 
JamesM said:
Java development is extremely painful without an IDE. Eclipse works for me. Since I started using it, package import pain is a thing of the past.
Will Eclipse let me use my own editor? If not, forget it. I've got 15 years invested in it.

~~ Paul
 

Back
Top Bottom