a simple Web server will be built from listing 9.2. Of course, the method and response events must also be improved. A simple server does not parse and store the request headers. The new Web server will analyze and store requests for future processing. In order to achieve this goal, You must have a class that contains HTTP requests.
HttpRequest class
Listing 9.5 Lists a complete HttpRequest class. This class must include all the information required by a request header.
list 9.5.HTTPrequest class.
import java.io.*;
import java.util.*;
import java.net.*;
import Namevalue;
/**
* This class has all the information for an HTTP request
*/
public class HttpRequest
{
public String version;
public String method;
public String file;
public Socket Clientsocket;
public DataInputStream Inbound;
public Namevalue headerpairs[];
/**
* Create an instance of this class
*/
public HttpRequest ()
{
version = NULL;
method = null;
file = null;
clientsocket = null;
inbound = null;
inbound = null;
headerpairs = new Namevalue[0];
}
/**
* Add a name/value pair to the core array
*/
public void Addnamevalue (string name, string value)
{
Try
{
namevalue temp[] = new namevalue[headerpairs.length + 1];
temp[Headerpairs.length] = new Namevalue (name, value);
headerpairs = temp;
}
catch (nullpointerexception NPE)
{
System.out.println ("nullpointerexception while adding Name-value:
"+ NPE);
}
}
/**
* returns this class in string form
*/
Public String toString ()
{
String s = method + "" + file + "" + version + \ n;
for (int x = 0; x < Headerpairs.length + +)
s + = headerpairs[x] + "\ n";
return s;
}
}
The
Namevalue class simply stores two strings: name and value. When a new pair is to be added, a new array is assigned. The new array accepts the old array and new members. The old array is then overwritten by a newly created object.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service