WebClient: Talking to Web Servers Interactively

By: aathishankaran Emailed: 1730 times Printed: 2345 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).

 

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

}


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

Comments(0)


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 CheckBox Example
Comparison operators in JSP
JSP Program for display Date
Uploading an Image to a Database using JSP
Sending Email using JSP
Embedding java codes in jsp sciptlets
Enable/Disable Scripting Elements in JSP
The Request Object in JSP
Tags using in jsp
Automatically Refreshing a JSP
Calling JSP from a batch file
Writing your first JSP page
Most Emailed Articles (in JSP)
Techniques for form editing
Password Object
Embedding a QuickTime movie in a JSP
Scriptlets and Expressions in JSP
What is JSP?
Scriptlets
JSP Program for display Date
Protecting your website with a login page
Sessions in JSP
Tag libraries
Tags in JSP
Basic Servlet Structure
Debugging Servlets
Select Object
The best way to avoid security vulnerabilities with new server