Solved

Browser Document Output from Java Applet

Posted on 1997-06-19
12
308 Views
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 :

      document.writeln(.....)

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
0
Comment
Question by:kitbishop
[X]
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
12 Comments
 
LVL 6

Expert Comment

by:jpk041897
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.


or

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;

try{
   URL myUrl = new URL("http://www.netscape.com/index.html");
} catch {MalfomredURLException e){
// Whatever error procesing you want here
}
getAppletContext.showDocument(myURL);

...


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".
0
 

Author Comment

by:kitbishop
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.open();
   document.writeln("<html><body>Hi there</body></html>");
   document.close();

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 :

    document.open();
    document.writeln(document.appletname.getit());
    document.close();

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

0
 
LVL 6

Expert Comment

by:jpk041897
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.
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:kitbishop
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(...)'
0
 
LVL 6

Expert Comment

by:jpk041897
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. :-)
0
 

Author Comment

by:kitbishop
ID: 1854328
I really would like to get this problem solved!!!!
0
 
LVL 2

Expert Comment

by:Philippe
ID: 1854329

kitbishop,

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,

   Philippe


0
 

Author Comment

by:kitbishop
ID: 1854330
Phillippe,

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
0
 
LVL 1

Expert Comment

by:byoung
ID: 1854331
Here's what I would recommend.  The hierarchy for applets is odd, so check out:
http://java.sun.com/products/jdk/1.1/docs/api/Package-java.applet.html
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
0
 

Author Comment

by:kitbishop
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.
0
 
LVL 2

Accepted Solution

by:
Philippe earned 150 total points
ID: 1854333

Kit,

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,

   Philippe
   
0
 

Author Comment

by:kitbishop
ID: 1854334
Phillipe,

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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

707 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