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
Solved

Opening a Web page from a java application

Posted on 2004-08-10
9
332 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
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.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
What's wrong with this code? 4 26
difference between sorce folder and folder in eclipise 3 28
Java Eclipse Loop 3 21
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

837 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