Java Socket programming (5) Simple WEB Server

Source: Internet
Author: User

 

 

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.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.