1

I am still pretty new to Java and for my class, we have to make a web browser without JEditor. I just need to figure out how to use the socket when a user types in the JTextField and then make it go to that website. Also, I am only supposed to use http:// not https:// if that makes a difference. Anything helps! Thanks!

    import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
public class WebPanel extends JPanel{
PrintWriter pw;
JTextField text = new JTextField("http://www.");

public WebPanel(){

 //trying to connect to the website 
    try {
     Socket s = new Socket(text, 80); //<<<---where the error is thrown
     OutputStream os = s.getOutputStream();
     pw = new PrintWriter(os, true);
     pw.flush();
  } catch (Exception e) {
    e.printStackTrace();
  }
    //set layout
    setLayout(null);

    ButtonHandler bh = new ButtonHandler();

   text.addActionListener(bh);
   text.setBounds(185, 10, 315, 25);
   add(text);

    //buttons 
    JButton goButton = new JButton("GO");
    goButton.addActionListener(bh);
    add(goButton);
    goButton.setBounds(500, 10, 75, 25);

    JButton backButton = new JButton("BACK");
    backButton.addActionListener(bh);
    add(backButton);
    backButton.setBounds(2, 10, 75, 25);
    JButton forwardButton = new JButton("FORWARD");
    forwardButton.addActionListener(bh);
    add(forwardButton);
    forwardButton.setBounds(80, 10, 100, 25);
    }
     public class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
     //buttons for later
        if(e.getActionCommand().equals("GO")){
            String input = text.getText();
            System.out.println("You searched for: " + input);
            pw.flush();
        }else if(e.getActionCommand().equals("BACK")){
            System.out.println("you pressed the back button");
            pw.flush();
        } else if(e.getActionCommand().equals("FORWARD")){
            System.out.println("You pressed the forward button");
            pw.flush();
        }
    }

    }
    }
    }
Anya
  • 21
  • 4
  • Your question is too broad. It's like asking an engineer "How to make a car?". Break your problem down into pieces. Get the user input. Make an HTTP Request. Display the page. Does it work? Where did you get stuck? – mvd Oct 22 '15 at 23:03
  • Share some code that you have working so far, or a snippet of code that you are trying to get working. It will be easier for others to provide help if you can post more specific problems/questions. – Kevin Hooke Oct 22 '15 at 23:13
  • I updated it, thanks for helping me out! – Anya Oct 22 '15 at 23:56
  • So why do you put variable of JTextField into Socket constructor instead of getting String from it using `text.getText()`? – rsutormin Oct 23 '15 at 22:53
  • Another thing is when you use Socket you can not use URLs like you define in `JTextField` (I mean "http://..."). In Socket you define host name and port. No protocol, no relative resource path. All them you should provide by writing into socket itself. – rsutormin Oct 23 '15 at 22:56
  • Is there any reason why you have to do it through sockets and can not use easier approach with `URL.openStream` from my answer? – rsutormin Oct 23 '15 at 22:59
  • This example could be similar to what you're searching for: http://stackoverflow.com/questions/10673684/send-http-request-manually-via-socket – rsutormin Oct 23 '15 at 23:02
  • I used JTextField("http://") to make it where someone can just type right after it. Not because I thought it would work that way? – Anya Oct 25 '15 at 06:20
  • I didn't know another way to do it. I have only been taught sockets, so that's what I thought I was supposed to do. – Anya Oct 25 '15 at 06:21
  • I really appreciate the comments. You're suggestions had really helped. Thanks! – Anya Oct 25 '15 at 06:23

1 Answers1

0

For simple cases you don't need to use Sockets. You can use URL class like this:

import java.io.InputStream;
import java.net.URL;
import org.apache.commons.io.IOUtils;

public class URLReadTest {
    public static void main(String[] args) throws Exception {
        InputStream is =  new URL("http://stackoverflow.com/").openStream();
        String html = IOUtils.toString(is);
        is.close();
        System.out.println(html);  // Parse HTML here
    }
}

And then you have to parse returned HTML by yourself before you can visualize it of course.

rsutormin
  • 1,629
  • 2
  • 17
  • 21