Browser Document Output from Java Applet

I'd be most greatful if anyone could help me with the following problem.

In JavaScript, I can output to the current browser document using :


I would really like to be able to do the same thing from a Java Applet but
have not found a way to do so.  Does anyone have any ideas on how it can be
done, or point me at any useful web page for reference.

I have managed to produce the effect I want by using JavaScript to call an
applet method that returns the text I want to write, but I really would
like to bypass this extra step.

Thanks in anticipation of helpful replies.

Best regards,

Kit Bishop,
Bishop Consulting
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

For an applet to display the contents of a given url you can use one of two methods:

void showDocument(URL url)

Shows the specified document in the current context.


void showDocument(URL url, String target)

Requests that the browser show the specified document in a particular location—either a browser window or a Netscape frame.

Options for target:
Value Document Displayed in:

"_self" Current Frame
"_parent" Parent Frame
"_top" Top-most Frame
"_blank" In a new and unnamed browser window
"aNameofYourChoice" Creates a new browser window with the specified name.  You may later display other documents in this window by using the same name as the target.

As an example, you could use:


   URL myUrl = new URL("");
} catch {MalfomredURLException e){
// Whatever error procesing you want here


Important Note:

Due to bugs in diffrent browser, and browser version, implementations, the only two target values that you can be sure will run as documented on all Java enabled browsers are "_self" and "_parent".
kitbishopAuthor Commented:
While appreciating jpk's answer, the material presented is familiar to me and does not solve my problem.

In JavaScript, I can generate any output I want to display in the browser.  For example, the JavaScript :;
   document.writeln("<html><body>Hi there</body></html>");

will display in the browser as a new page with just the words :

   Hi there

I would like to be able to do the same direct from the applet.

At present, my solution is something like :

Applet method :

    public String getit()
        return "....."

and JavaScript code :;

I really would like to be able to do this without having to have the embedded JavaScript.

I.E. I want an applet equivalent to the JavaScript document.writeln

I suggest you grade my answer with and F and see if anyone else has figured out an alternative that can run on all Java enabled browsers.

Unfortunatley for your purposes,applet is only available as an HTML tag, and therfore was not designed to return a value that a Script could use.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

kitbishopAuthor Commented:
Many thanks to jpk's attempts at answering my question, however....

Just by way of further clarification,  what I need to do is not to get access to the applet from the HTML script, but rather to get access to the script variable 'document' from within an applet so I can perform, within the applet, the equivalent to the script command 'document.writeln(...)'
You need to grade the answer with an F or it will continue to appear in the section of questions awaiting evaluation. It will therfore be skipped by most experts:-(.

Please grade it F so that other people will take a look at your qustion. :-)
kitbishopAuthor Commented:
I really would like to get this problem solved!!!!


java does not allow to send html directly from an applet to the browser. The standard way would be to use javascript. If you have to do it in java here is a way around it:

Have your applet send the html to a cgi-bin on the server it came from and then use ShowDocument(URL) do retrieve the HTML from the server.

I'm afraid that is only way to do it in java. Note that it is not is not very safe since people could use your cgi-bin to fill up your hard disk.

 hope this helps,


kitbishopAuthor Commented:

Sorry for taking so long to get back to you on this, I've not been around for a couple of days.

Clearly, your answer was not what I wanted to hear :-( though I feared it might be the case.

Unfortunately, your suggested solution is not viable for a variety of reasons.

I have failed your answer only in the very vague hope that some one else might have a bright idea.  If within a couple of days I have not received any other satisfactory answer, if you re-answered this with some brief comment, I would give you a non-fail grading as you have genuinely attempted an answer.

Kit Bishop
Here's what I would recommend.  The hierarchy for applets is odd, so check out:
because what you might be able to do, is use the paint method for the container class.  I've never tried this before, but this(and the call to JS) are the only feasible options.  Try this page, and cruise around the applet part,,also check out the awt section, it might help.  Good Luck
kitbishopAuthor Commented:
Thanks for all the various suggestions - I've just about given up on a workable solution and think I will have to stick with calling applet methods from JavaScript to do what I want ... shame :-(

You would have thought that the JavaScript objects would be available from within an applet - does anyone have any bright ideas as to how it could be done - sorry I can't pay for it ;-) but it might be an intersting project to encapsulate the JavaScript objecys as classes - if it's feasibale at all.


JavaScript and Java are quite different in the way they interact with your browser. Java applets are constrained to play in a sandbox as they say, meaning they can only act on the frame in which the applet its being displayed and can only communicate withe the host they have been downloaded from. Thus encapsulating JavaScript into Java can't really work.

Now, if you have two applets on the same html page, they can find out about each other and can communicate by calling each others methods.

Netscape has an extension to that which allows applets to call JavaScript which are on the same page as the applet (it is called livetalk or hottalk?). This could be a way of getting the best of both worlds.

 hope this helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kitbishopAuthor Commented:

Thanks, I understand what you are saying - I'd basically figured that out myself.  Unfortunately livetalk/hottalk is nio use to me since I want to be able to operate on any common browser that accepts javascript and applets.

Since this is the second time you have attempted to answer this question for me, I'm happy you should get some points for your efforts - thanks.

PS : In doing the work, I have found a rather annoying (but surmountable) difference between MSIE and Netscape, namely that in Netscape, when you do a document.write from javascript, any applet it the same page is stopped.  In MSIE, the applet keeps going until you do a document.close.  Since I wish to do document.write multiple times from data obtained from the applet, this is annoying.  To be compatible, I have to accumulate all the data from the applet in javascript variables before doing any document.write.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.