Web Browser Write a very simple GUI web browser. Your program should have a text
ID: 3808423 • Letter: W
Question
Web Browser
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 URL’s 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 Don’t 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 doesn’t want to see this stuff Javascript definitions Other stuff the user doesn’t care about. 2 Here’s all the stuff the user really cares about.
Along with a bunch of formatting,
Web BrowserExplanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
public class WebBrowser extends JFrame implements ActionListener,HyperlinkListener
{
private JEditorPane pane;
private JScrollPane spane;
private Button b1;
private Button b2;
private Button b3;
private TextField addr_bar;
private URL url;
public WebBrowser()
{
super("WebBrowser");
setSize(950,700);
Container c=getContentPane();
b3=new Button("G0");
b3.addActionListener(this);
b1=new Button("Close");
b1.addActionListener(this);
b2=new Button("Def");
b2.addActionListener(this);
addr_bar=new TextField("http://www.");
addr_bar.addActionListener(this);
pane=new JEditorPane();
pane.setEditable(false);
pane.addHyperlinkListener(this);
c.setLayout(null);
spane=new JScrollPane(pane);
spane.setBounds(10,50,940,650);
addr_bar.setBounds(10,20,740,25);
b3.setBounds(751,20,50,25);
b1.setBounds(803,20,50,25);
b2.setBounds(855,20,50,25);
c.add(b1);
c.add(spane);
c.add(b3);
c.add(b2);
c.add(addr_bar);
show();
}
public void actionPerformed(ActionEvent e)
{
setTitle("My Web Browser");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Object act=e.getSource();
String str=addr_bar.getText();
if (act==b3 || act==addr_bar)
{
try {
pane.setPage(str);
setTitle("My Web Browser");
}
catch (IOException ei)
{
try {
pane.setPage("http://www.jksportshub.com");
addr_bar.setText("JkSportshub");
}
catch (IOException se) {
System.out.print("Exception");
}
}
}
else if (act==b1)
{
System.exit(0);
}
else if (act=="About")
{
JOptionPane.showMessageDialog( this,"Web Brow0.1","Hello ",JOptionPane.INFORMATION_MESSAGE );
}
}
public void hyperlinkUpdate( HyperlinkEvent ea )
{
if ( ea.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
{
try {
pane.setPage( ea.getURL().toString() );
addr_bar.setText(ea.getURL().toString());
}
catch (IOException ei)
{
try {
pane.setPage("http://www.jksportshub.com");
addr_bar.setText("Default page");
}
catch (IOException se) {
System.out.print("JKSportsHub");
}
}
}
}
public static void main(String[] args)
{
WebBrowser web=new WebBrowser();
web.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.