Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes View HTML source using Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "View HTML source using Java?" Watch "View HTML source using Java?" New topic
Author

View HTML source using Java?

Paul Smith
Greenhorn

Joined: Jan 27, 2005
Posts: 8
Hello People,

Can you guide me as to what methods I'll need (it's doing my head in for some reason) to use in other to view an HTML source code.

NB: As I am learning PLEASE don't just give me the code on a plate. Advise, guide, direction etc, will be most appreciated.

Thanks a million.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

What's the context? A user with a foo.html file on their hard disk? A web browser?

When you say "view HTML source code" that generally means to view the actual contents of the HTML as plain text; but I should make sure you don't mean render the HTML, as a web browser would.

So explain a bit more of what you want to do, and I'm sure we'll be able to help you.


[Jess in Action][AskingGoodQuestions]
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Paul Smith:
Hello People,

Can you guide me as to what methods I'll need (it's doing my head in for some reason) to use in other to view an HTML source code.

NB: As I am learning PLEASE don't just give me the code on a plate. Advise, guide, direction etc, will be most appreciated.

Thanks a million.


As an aside, I would like to applaud you for taking the initiative to learn this for yourself rather than asking others to "give you the code on a plate." There's nothing wrong with asking for a little guidance now and then. If you take the time to answer Mr. Friedman-Hill's (pay attention to this guy. I've read his book on JESS and he's very sharp and very informative) question(s), I'm sure one of us can help direct you on your way.


James Carman, President<br />Carman Consulting, Inc.
Paul Smith
Greenhorn

Joined: Jan 27, 2005
Posts: 8
Hello guys (Ernest Friedman-Hill & James Carman),

What I mean is that when a user types in a wb address(from command promt maybe), the program should return the HTML source code for that website.

So far I can mimic the unix 'nslookup' program ie: return the IP or Host name of hostnames/IPs entered by the user. I think it might be similar, maybe that's where I'm goin wrong.

I am listening for user inputs using:

String readFromKeyboard;

BufferedReader connectsKeyboard = new BufferedReader(new InputStreamReader(System.in));
readFromKeyboard = connectsKeyboard.readLine();

I have instantiated URL:

URL aSite = new URL (readFromKeyboard);
System.out.println("I can't seem to find this magic method " + aSite.getFile());

At this point im just picking out methods all over the place. getFile() is just one of many I've tried...getContent() & so on.

I hope I'm a bit clearer now - thanks again people.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Read up on the java.net.URLConnection class.
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLConnection.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
More specifically, check out its subclass HttpURLConnection. When you ask for a connection from your URL, you'll have to cast it to an HttpURLConnection. That class has methods specific to the HTTP protocol on it that you might find useful.
Paul Smith
Greenhorn

Joined: Jan 27, 2005
Posts: 8
I have sorted out my program - it does what I want...

It's all in the API...I just have to understand how to use the API properly.

Thanks to you all..

PS: It was all mainly to do with BufferedReader, InputStreamReader & getInputStream.
 
Consider Paul's rocket mass heater.
 
subject: View HTML source using Java?