WebClient: Talking to Web Servers Interactively

By: aathishankaran Emailed: 1732 times Printed: 2341 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing


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).




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.”




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,


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) {



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);




Label requestHeadersLabel =

new Label("Request Headers:");



inputPanel.add(labelPanel, BorderLayout.NORTH);

requestHeadersArea = new TextArea(5, 80);


inputPanel.add(requestHeadersArea, BorderLayout.CENTER);

Panel buttonPanel = new Panel();

submitButton = new Button("Submit Request");




inputPanel.add(buttonPanel, BorderLayout.SOUTH);

add(inputPanel, BorderLayout.NORTH);

Panel resultPanel = new Panel();

resultPanel.setLayout(new BorderLayout());

Label resultLabel =

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


resultPanel.add(resultLabel, BorderLayout.NORTH);

resultArea = new TextArea();


resultPanel.add(resultArea, BorderLayout.CENTER);

Panel interruptPanel = new Panel();

interruptButton = new Button("Interrupt Download");




resultPanel.add(interruptPanel, BorderLayout.SOUTH);

add(resultPanel, BorderLayout.CENTER);

setSize(600, 700);



public void actionPerformed(ActionEvent event) {

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

Thread downloader = new Thread(this);


} 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() {



private boolean hasLegalArgs() {

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

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

report("Missing hostname");



JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
JSP Alert Example
What are the different scopes in JSP?
JSP Program for display Date
JSP CheckBox Example
Comparison operators in JSP
Sending Email using JSP
Arithmetic Evaluation Using the Expression Language in JSP
The Request Object in JSP
Embedding java codes in jsp sciptlets
A Simple Servlet Generating Plain Text
What is JSP?
Uploading an Image to a Database using JSP
Getting Started with JSP
The page Directive in JSP
Most Emailed Articles (in JSP)
Sending Email using JSP
WebClient: Talking to Web Servers Interactively
JSP Program for display Date
The Servlet Life Cycle
Checkbox Object
Select Object
Referencing Windows
Calling JSP from a batch file
Embedding a QuickTime movie in a JSP
Embedding MP3 audio file in a JSP
Declarations in JSP
Tag Libraries in JSP
Querying Data with the JSTL in JSP