The Artima Developer Community
Sponsored Link

Java Answers Forum
opening web pages from an applet

6 replies on 1 page. Most recent reply: Mar 10, 2002 7:07 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

opening web pages from an applet Posted: Mar 10, 2002 12:54 AM
Reply to this message Reply
Advertisement
I have some questions on applets and web pages:

- How can I check (boolean) if a web page exists?
- How do I open 'local' URLs in my browser (not starting with http://, just a filename)?

Thanks!


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: opening web pages from an applet Posted: Mar 10, 2002 2:15 AM
Reply to this message Reply
Here is a little sample applet that shows (just the bare essentials of) opening a web page in the browser, with a local file that has space characters in the path. I'm not sure if this will work if the applet is not running from your local system, though. Of course, if it is on a server somewhere, the chances it knows the locations of local html files on your system are slim (although, maybe you have a text field where someone can type in the page they want to go to, or something).
import javax.swing.*;
import java.awt.*;
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.net.MalformedURLException;
 
public class OpenLocalPage extends JApplet
{
    Container pane;
    public void init()
    {
        pane = getContentPane();
        JButton button = new JButton( "Browse Somewhere!" );
 
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                URL url = null;
                try
                {
                    url = new URL( "file:///D|/reference/Java%201.3%20Docs/api/index.html" );
                }
                catch( MalformedURLException mfue )
                {
                    // TODO: Handle this exception appropriately!
                }
                if(url != null)
                    getAppletContext().showDocument(url);
            }
        } );
        pane.add( button );
    }
}

Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

Re: opening web pages from an applet Posted: Mar 10, 2002 2:41 AM
Reply to this message Reply
Hi.
I tried this:

String temp = "file:///"+tekst.getText()+".html";
try{
adres = new URL(temp);
}
catch (MalformedURLException mfue){
}

and the file c:\tekst.getText().html is openend, instead of c:\localsubdirectory\tekst.getText().html.
This is rather important if I want to use this on the web.

Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

Re: opening web pages from an applet Posted: Mar 10, 2002 2:49 AM
Reply to this message Reply
Ok, this won't work on a server... Maybe someone can help me: I need an applet that opens the file ***.html in the local subdirectory (on the web) when someone enters *** end presses a button.

Pieter Provoost

Posts: 9
Nickname: pieter
Registered: Mar, 2002

Re: opening web pages from an applet Posted: Mar 10, 2002 2:55 AM
Reply to this message Reply
So I need a way to find out the path http://... of the document displaying the applet. That can't be hard I suppose?

And I am still looking for a way to find out if a page exists...

Thanks for your time!

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: opening web pages from an applet Posted: Mar 10, 2002 6:55 AM
Reply to this message Reply
The applet method getDocumentBase() should work for getting the url of the applet.



getDocumentBase

public URL getDocumentBase()

Returns an absolute URL naming the directory of the document in which the applet is embedded. For example, suppose an applet is contained within the document:

http://java.sun.com/products/jdk/1.2/index.html


The document base is:

http://java.sun.com/products/jdk/1.2/

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: opening web pages from an applet Posted: Mar 10, 2002 7:07 AM
Reply to this message Reply
you could use a try catch block approach in a method with a boolean return value.

The try block should return true if the page exists and false if the catch block is executed.
The try block could consist of creating a url

then either attempting to get the contents of the url
which would cause an IOException if not really there
with the url method getContent()


getContent

public final Object getContent()
throws IOException

Gets the contents of this URL. This method is a shorthand for:

openConnection().getContent()


Returns:
the contents of this URL.
Throws:
IOException - if an I/O exception occurs.
See Also:
URLConnection.getContent()

or trying to get the file name with getFile() and setting the method return value to false if an empty string is returned.


getFile

public String getFile()

Gets the file name of this URL.


Returns:
the file name of this URL, or an empty string if one does not exist

Flat View: This topic has 6 replies on 1 page
Topic: Where does object live? Previous Topic   Next Topic Topic: add textarea as a node to jtree

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use