FileDocCategorySizeDatePackage
WebServerFrame.javaAPI DocExample3103Thu Nov 08 00:23:26 GMT 2001com.ora.rmibook.chapter2

WebServerFrame

public class WebServerFrame extends ExitingFrame

Fields Summary
private JTextArea
_displayArea
private static final String
_mainPage
Constructors Summary
Methods Summary
protected voidbuildGUI()

        JPanel newContentPane = new JPanel(new BorderLayout());

        newContentPane.add(buildMainPanel(), BorderLayout.CENTER);
        setContentPane(newContentPane);
        setSize(250, 300);
    
private javax.swing.JComponentbuildMainPanel()

        _displayArea = new JTextArea();
        _displayArea.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(_displayArea);

        scrollPane.setBorder(new EtchedBorder());
        return scrollPane;
    
private voidprocessClientRequest(java.net.Socket client)

        _displayArea.append("Client connected from port " +
            client.getPort() + " on machine " + client.getInetAddress() + "\n");
        _displayArea.append("Request is: \n");
        readRequest(client);
        sendResponse(client);
        client.close();
    
private voidreadRequest(java.net.Socket client)

        BufferedReader request = null;

        request = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String nextLine;

        while (null != (nextLine = request.readLine())) {
            // Ideally, we'd do something. But this is a very
            // simple web server.
            if (nextLine.equals("")) {
                break;
            } else {
                _displayArea.append("\t" + nextLine + "\n");
            }
        }
        _displayArea.append("---------------------------------------\n");
        return;
    
private voidsendResponse(java.net.Socket client)

        BufferedWriter response;

        response = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        response.write(_mainPage);
        response.flush();
    
public voidstartListening()

        ServerSocket serverSocket;

        try {
            serverSocket = new ServerSocket(80);
        } catch (IOException e) {
            return;
        }
        while (true) {
            try {
                Socket client = serverSocket.accept(); // wait here

                processClientRequest(client);
                //bad design-- should  handle requests in separate threads
                // and immediately resume listening for connections            	client.close();
            } catch (IOException e) {
            }
        }