Solved

Opening a Web page from a java application

Posted on 2004-08-10
9
330 Views
Last Modified: 2010-03-31
Hi!

I am looking for some example java code, which will allow me to connect to a specific webserver, and return the page as a string, which I can use in my java application..

Many Thanks,

Chris
0
Comment
Question by:chrishughes
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 50 total points
ID: 11761110
like this: ?

---------------------

import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class HtmlPanel extends JPanel implements HyperlinkListener {
    protected JEditorPane html;

    public HtmlPanel(String startURL) {
        setLayout(new BorderLayout());

        try {
            URL url = new URL(startURL);
            html    = new JEditorPane(url);
            html.setEditable(false);
            html.addHyperlinkListener(this);
            JScrollPane scroller = new JScrollPane();
            JViewport vp = scroller.getViewport();
            vp.add(html);
            vp.setBackingStoreEnabled(true);
            add(scroller, BorderLayout.CENTER);
 
        } catch (MalformedURLException e) {
            System.out.println("Malformed URL: " + e);
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    }

    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            System.out.println("myMess"+e.getURL());
            linkActivated(e.getURL());
        }
    }

    protected void linkActivated(URL u) {
        Cursor c = html.getCursor();
        Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
        html.setCursor(waitCursor);
        SwingUtilities.invokeLater(new PageLoader(u, c));
    }

    class PageLoader implements Runnable {
        URL url;
        Cursor cursor;

        PageLoader(URL u, Cursor c) {
            url = u;
            cursor = c;
        }
        public void run() {
            if (url == null) {
                html.setCursor(cursor);
                Container parent = html.getParent();
                parent.repaint();
            } else {
                Document doc = html.getDocument();
                try {
                    html.setPage(url);
                } catch (IOException ioe) {
                    html.setDocument(doc);
                    getToolkit().beep();
                } finally {
                    url = null;
                    SwingUtilities.invokeLater(this);
                }
            }
        }
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame("Roadster");
        HtmlPanel panel = new HtmlPanel(args[0]);

        frame.getContentPane().add(panel);
        frame.setSize(500, 300);
        frame.setVisible(true);
    }
}
0
 

Author Comment

by:chrishughes
ID: 11761201
Kind of - But I am not looking for any form of graphical solution. I just want to return the page as a text string to my command prompt based application.
0
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 11761215
0
 
LVL 4

Assisted Solution

by:alikoank
alikoank earned 25 total points
ID: 11761255
 public String readURL(String url) {
    StringBuffer buf = new StringBuffer();
    String s=null;
    try {
      URL u = new URL(url);
      BufferedReader in = new BufferedReader(
          new InputStreamReader(u.openStream()));
      while ( (s = in.readLine()) != null) {
        buf.append(s);
      }
    } catch (MalformedURLException ex) {
    } catch (IOException ex) {
    }
    return buf.toString();
  }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 50 total points
ID: 11761258
http://javaalmanac.com/egs/java.net/ReadFromURL.html

------------

try {
        // Create a URL for the desired page
        URL url = new URL("http://hostname:80/index.html");
   
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println( str ) ;
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
0
 

Author Comment

by:chrishughes
ID: 11761301
That looks like the solution - however which packages do I need to import, to get it to work?

Thanks
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 50 total points
ID: 11761315
java.net.*;
java.io.*;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11761367
:-)

Good luck!

Tim
0
 
LVL 35

Expert Comment

by:girionis
ID: 11761376
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now