Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Browser Document Output from Java Applet

Posted on 1997-06-19
Medium Priority
Last Modified: 2013-12-25
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
E-mail cbishop@voyager.co.nz
Question by:kitbishop
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1

Expert Comment

ID: 1854323
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("http://www.netscape.com/index.html");
} 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".

Author Comment

ID: 1854324
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


Expert Comment

ID: 1854325
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 1854326
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(...)'

Expert Comment

ID: 1854327
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. :-)

Author Comment

ID: 1854328
I really would like to get this problem solved!!!!

Expert Comment

ID: 1854329


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,



Author Comment

ID: 1854330

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

Expert Comment

ID: 1854331
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

Author Comment

ID: 1854332
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.

Accepted Solution

Philippe earned 450 total points
ID: 1854333


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,


Author Comment

ID: 1854334

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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question