SimpleWEB Server
A simple WEB server will be built by listing 9.2. of course, the other party's method and response events must also be improved. simple servers do not analyze and store request headers. the new WEB server will analyze and store requests to prepare for future processing. for this purpose, you must have a class containing HTTP requests.
HTTPrequest class
List 9.5 lists a complete HTTPrequest class. This class must contain 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 of 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];
System. arraycopy (headerpairs, 0, temp, 0, headerpairs. length );
Temp [headerpairs. length] = new NameValue (name, value );
Headerpairs = temp;
}
Catch (NullPointerException npe)
{
System. out. println ("NullPointerException while adding name-value:
"+ Npe );
}
}
/**
* Return this class as a string
*/
Public String toString ()
{
String s = method + "" + file + "" + version + "";
For (int x = 0; x S + = headerpairs [x] + "";
Return s;
}
}
The NameValue class simply stores two strings: name and value. when a new pair is added, a new array is allocated. the new array accepts the old array and new members. the old array is then overwritten by a new object.