Question: Hi, this if for Java. Any help would be amazing. Write a very simple GUI web browser. Your program should have a text edit box

Hi, this if for Java. Any help would be amazing.

Write a very simple GUI web browser. Your program should have a text edit box at the top of the window for the user to type a URL. Provide a listener that activates when the user types something in and presses Enter. The HTTP specification is fairly simple. Create a Socket object from the URLs web address, connecting on port 80 (the default HTTP port). You will probably want to attach a PrintWriter and a BufferedReader to the Socket. Send the following lines of text, ending in both carriage return and newline characters (yes, HTTP follows Windows line-ending conventions, rather than Unix):

GET HTTP/1.1

Host:

In other words, if the user typed http://cs.okstate.edu/students.html, your program should send these three lines to the Socket:

GET /students.html HTTP/1.1

Host: cs.okstate.edu

Dont forget to flush your output afterwards, or your request will never be sent. The web server will respond with a bunch of text including the web page, which you should read into a String. In the program panel, display (as plain text) the body of the webpage (explained below). Make sure that you set up the display so that it includes a scroll bar when needed. You must also handle exceptions and errors gracefully. If the user enters a nonexistent URL, for instance, or if the HTTP response is formatted incorrectly, then the program should inform the user. A proper HTTP response will look like the following:

HTTP header junk

Several lines

User doesnt want to see this stuff

Javascript definitions

Other stuff the user doesnt care about.

The Title of the Webpage

Heres all the stuff the user really cares about.

Along with a bunch of formatting,

links,

and images.

Browsers display HTML by following the instructions provided by tags delineated by angle brackets. The first word inside a bracket specifies the kind of tag; tags can then have other information inside, and then end with a closing bracket. All of the text after a tag is formatted according to the tags instructions, until a closing tag is encountered. These tags are also delineated by angle brackets, and include a slash and then the name of the tag. Thus, in the example, the line Along with a bunch of formatting, is enclosed by the h1 tag, which instructs the browser to display it with a font size indicating that it is a first-level heading. According to the HTML specification, browsers are not required to implement all of the scores of tags that exist, and they should ignore any tags they do not recognize. Your browser will ignore almost all of them. It will be able to do the following:

Extract the webpages title from between the

tags and change the title of your program frame appropriately.

Display all of the text located between the

and tags, and nothing outside of the body.

Ignore every other tag. Dont print out any angle brackets or any of the text located inside of them. Needless to say, once you have the webpage loaded into a String, you will be using a lot of substring() and charAt() calls to massage the response into the webpage you will display to the user.

Note: Java has a few interesting classes that will render HTML themselves, such as the JEditorPane. Theyre fun to play around with, and might even be useful to you as a first step to getting your program working. However, for the purposes of this assignment, you must implement your own simple renderer, working from the simple string of plain text you get from a web server.

Note: More and more web pages are using the encrypted Secure HTTP protocol, which is denoted https in the URL. Your browser will be unable to access these web pages (including common ones like google.com). Dont assume your browser is broken when testing your code until you have verified that you are not trying to access an encrypted page. Pages on the cs.okstate.edu site are unencrypted, so theyre a good choice to test with.

Extra Credit Implement another few HTML tags. Some suggestions: Headings (

,

, etc) should change the font size. Images () could be displayed via Toolkit.getImage(URL). Hyperlinks () could be implemented as buttons or clickable text (this is nontrivial, but it would make your browser into a true websurfing app, which would be pretty cool).

This is what I have so far:

//--------------------------------------Browser.java

public class Browser { public static void main(String[] args) { BrowserFrame browserFrame = new BrowserFrame(); } }

//-------------------------------------------------BrowserFrame.java

import javax.swing.*; import java.awt.*;

public class BrowserFrame extends JFrame { public String enteredURL = ""; BrowserFrame(){ setTitle("Browser"); setSize(800, 800); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); BrowserPanel browserPanel = new BrowserPanel(); browserPanel.setFrame(this); add(browserPanel); setVisible(true); } }

//---------------------------------------------BrowserPanel.java

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

public class BrowserPanel extends JPanel {

BrowserFrame frame; PageContent pageContent; JTextArea textArea; JTextField urlTextField; JScrollPane scrollPane;

public BrowserPanel() { super(); setLayout(new BorderLayout());

urlTextField = new JTextField(); textArea = new JTextArea(); textArea.setLineWrap(true); scrollPane = new JScrollPane(textArea);

urlTextField.addActionListener(new UrlTextFieldHandler());

add(urlTextField, BorderLayout.PAGE_START); add(scrollPane, BorderLayout.CENTER); }

public void setFrame(BrowserFrame frame) { this.frame = frame; }

//FIXME Still needs actionListener for when a user hits enter

//----------------------------------------------and this is the very simple network reader I've been trying to base the rest of the program on

import java.io.*; import java.net.*; public class NetworkReaderExample { public static void main( String[] args ){ String line; try{ Socket socket = new Socket("cs.okstate.edu", 80); PrintWriter out = new PrintWriter(socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.print("GET / HTTP/1.1 "); out.print("host: cs.okstate.edu "); out.flush(); while((line = in.readLine()) != null ){ System.out.println(line); } }catch(Exception e){ e.printStackTrace(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!