Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Opening a Web page from a java application

Posted on 2004-08-10
9
Medium Priority
?
337 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
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 200 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 200 total points
ID: 11761215
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Assisted Solution

by:alikoank
alikoank earned 100 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 200 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…
Suggested Courses

609 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