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.
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.


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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

777 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