Howtotomcatworks (1)

Source: Internet
Author: User

Webserver is the simplest and tested. You can send basic documents and images back to the client. The connector and container are replaced by three sentences. All three key parts of web server are available.

 

Package ex01.pyrmont;

Import java.net. Socket;
Import java.net. ServerSocket;
Import java.net. InetAddress;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. io. IOException;
Import java. io. File;

Public class HttpServer {

/** WEB_ROOT is the directory where our HTML and other files reside.
* For this package, WEB_ROOT is the "webroot" directory under the working
* Directory.
* The working directory is the location in the file system
* From where the java command was invoked.
*/
Public static final String WEB_ROOT =
System. getProperty ("user. dir") + File. separator + "webroot ";

// Shutdown command
Private static final String SHUTDOWN_COMMAND = "/SHUTDOWN ";

// The shutdown command has ED
Private boolean shutdown = false;

Public static void main (String [] args ){
HttpServer server = new HttpServer ();
Server. await ();
}

Public void await (){
ServerSocket serverSocket = null;
Int port = 8080;
Try {
ServerSocket = new ServerSocket (port, 1, InetAddress. getByName ("127.0.0.1 "));
}
Catch (ioexception e ){
E. printstacktrace ();
System. Exit (1 );
}

// Loop waiting for a request
While (! Shutdown ){
Socket socket = NULL;
Inputstream input = NULL;
Outputstream output = NULL;
Try {
Socket = serversocket. Accept ();
Input = socket. getinputstream ();
Output = socket. getoutputstream ();

// CREATE request object and parse
// Connector Section
Request request = new request (input );
Request. parse ();

// Create response object
Response response = new response (output );
Response. setrequest (request );
// Connector ends
// Container starts work
Response. sendstaticresource ();
// The container is finished.

// Close the socket
Socket. Close ();

// Check if the previous URI is a shutdown command
Shutdown = request. getUri (). equals (SHUTDOWN_COMMAND );
}
Catch (Exception e ){
E. printStackTrace ();
Continue;
}
}
}
}

 

Package ex01.pyrmont;

Import java. io. InputStream;
Import java. io. IOException;

Public class Request {

Private InputStream input;
Private String uri;

Public Request (InputStream input ){
This. input = input;
}

Public void parse (){
// Read a set of characters from the socket
StringBuffer request = new StringBuffer (2048 );
Int I;
Byte [] buffer = new byte [2048];
Try {
I = input. read (buffer );
}
Catch (IOException e ){
E. printStackTrace ();
I =-1;
}
For (int j = 0; j <I; j ++ ){
Request. append (char) buffer [j]);
}
System. Out. Print (request. tostring ());
Uri = parseuri (request. tostring ());
}

Private string parseuri (string requeststring ){
Int index1, index2;
Index1 = requeststring. indexof ('');
If (index1! =-1 ){
Index2 = requeststring. indexof ('', index1 + 1 );
If (index2> index1)
Return requeststring. substring (index1 + 1, index2 );
}
Return NULL;
}

Public String geturi (){
Return URI;
}

}

 

Package ex01.pyrmont;

Import java. Io. outputstream;
Import java. Io. ioexception;
Import java. Io. fileinputstream;
Import java. Io. file;

/*
HTTP Response = status-line
* (General-header | response-header | entity-header) CRLF)
CRLF
[Message-body]
Status-line = http-version SP status-code SP reason-phrase CRLF
*/

Public class response {

Private Static final int buffer_size = 1024;
Request request;
Outputstream output;

Public Response (outputstream output ){
This. Output = output;
}

Public void setRequest (Request request ){
This. request = request;
}

Public void sendStaticResource () throws IOException {
Byte [] bytes = new byte [BUFFER_SIZE];
FileInputStream FCM = null;
Try {
File file = new File (HttpServer. WEB_ROOT, request. getUri ());
If (file. exists ()){
FS = new FileInputStream (file );
Int ch = FCM. read (bytes, 0, BUFFER_SIZE );
While (ch! =-1 ){
Output. write (bytes, 0, ch );
Ch = FCM. read (bytes, 0, BUFFER_SIZE );
}
}
Else {
// File not found
String errorMessage = "" +
"HTTP/1.1 404 File Not Found/r/n" +
"Content-Type: text/html/r/n" +
"Content-Length: 23/r/n" +
"/R/n" +
"<H1> hello "<Html> oye! </Html> ";
Output. write (errorMessage. getBytes ());
}
}
Catch (Exception e ){
// Thrown if cannot instantiate a File object
System. out. println (e. toString ());
}
Finally {
If (FS! = Null)
FCM. close ();
}
}
}

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.