Solved

Opening a Web page from a java application

Posted on 2004-08-10
9
328 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 4

Assisted Solution

by:alikoank
alikoank earned 25 total points
Comment Utility
 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
java.net.*;
java.io.*;
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
:-)

Good luck!

Tim
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

744 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

17 Experts available now in Live!

Get 1:1 Help Now