WebClient: Talking to Web Servers Interactively

By: aathishankaran Viewed: 153249 times  Printer Friendly Format    


 

This article presents the source code for the WebClient program discussed in article (Debugging Servlets) and used in article (An Example Using Servlet Initialization and Page Modification Dates) and extensively throughout the article (Using HTML Forms).

 

WebClient

 

This class is the top-level program that you would use. Start it from the command line, then customize the HTTP request line and request headers, then press “Submit Request.”

 

WebClient.java

 

import java.awt.*;

import java.awt.event.*;

import java.util.*;

 

public class WebClient extends CloseableFrame

implements Runnable, Interruptible, ActionListener {

public static void main(String[] args) {

new WebClient("Web Client");

}

 

private LabeledTextField hostField, portField,

requestLineField;

private TextArea requestHeadersArea, resultArea;

private String host, requestLine;

private int port;

private String[] requestHeaders = new String[30];

private Button submitButton, interruptButton;

private boolean isInterrupted = false;

public WebClient(String title) {

super(title);

setBackground(Color.lightGray);

setLayout(new BorderLayout(5, 30));

int fontSize = 14;

Font labelFont =

new Font("Serif", Font.BOLD, fontSize);

Font headingFont =

new Font("SansSerif", Font.BOLD, fontSize+4);

Font textFont =

new Font("Monospaced", Font.BOLD, fontSize-2);

Panel inputPanel = new Panel();

inputPanel.setLayout(new BorderLayout());

Panel labelPanel = new Panel();

labelPanel.setLayout(new GridLayout(4,1));

hostField = new LabeledTextField("Host:", labelFont,

30, textFont);

portField = new LabeledTextField("Port:", labelFont,

"80", 5, textFont);

 

requestLineField = new LabeledTextField("Request Line:", labelFont, "GET / HTTP/1.0", 50, textFont);

labelPanel.add(hostField);

labelPanel.add(portField);

labelPanel.add(requestLineField);

Label requestHeadersLabel =

new Label("Request Headers:");

requestHeadersLabel.setFont(labelFont);

labelPanel.add(requestHeadersLabel);

inputPanel.add(labelPanel, BorderLayout.NORTH);

requestHeadersArea = new TextArea(5, 80);

requestHeadersArea.setFont(textFont);

inputPanel.add(requestHeadersArea, BorderLayout.CENTER);

Panel buttonPanel = new Panel();

submitButton = new Button("Submit Request");

submitButton.addActionListener(this);

submitButton.setFont(labelFont);

buttonPanel.add(submitButton);

inputPanel.add(buttonPanel, BorderLayout.SOUTH);

add(inputPanel, BorderLayout.NORTH);

Panel resultPanel = new Panel();

resultPanel.setLayout(new BorderLayout());

Label resultLabel =

new Label("Results", Label.CENTER);

resultLabel.setFont(headingFont);

resultPanel.add(resultLabel, BorderLayout.NORTH);

resultArea = new TextArea();

resultArea.setFont(textFont);

resultPanel.add(resultArea, BorderLayout.CENTER);

Panel interruptPanel = new Panel();

interruptButton = new Button("Interrupt Download");

interruptButton.addActionListener(this);

interruptButton.setFont(labelFont);

interruptPanel.add(interruptButton);

resultPanel.add(interruptPanel, BorderLayout.SOUTH);

add(resultPanel, BorderLayout.CENTER);

setSize(600, 700);

setVisible(true);

}

public void actionPerformed(ActionEvent event) {

if (event.getSource() == submitButton) {

Thread downloader = new Thread(this);

downloader.start();

} else if (event.getSource() == interruptButton) {

isInterrupted = true;

}

}

public void run() {

isInterrupted = false;

if (hasLegalArgs())

new HttpClient(host, port, requestLine,

requestHeaders, resultArea, this);

}

public boolean isInterrupted() {

return(isInterrupted);

}

private boolean hasLegalArgs() {

host = hostField.getTextField().getText();

if (host.length() == 0) {

report("Missing hostname");

return(false);

}



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial