How to implement a simple http server in java

Http

HyperText Transfer Protocol (HTTP) is the most widely used network protocol on the Internet. All WWW files must comply with this standard. The original purpose of designing HTTP was to provide a way to publish and receive HTML pages. In 1960, American Ted Nelson conceived a method of processing text information through a computer, and called it hypertext, which became the basis for the development of the HTTP hypertext transfer protocol standard architecture. The Ted Nelson organization and the World Wide Web ConsorTIum collaborated with the Internet Engineering Task Force to produce a series of RFCs, including the well-known RFC 2616, which defines HTTP 1.1.

Java

Java is an object-oriented programming language. It not only absorbs the various advantages of the C++ language, but also eliminates the concepts of multiple inheritance and pointers that are difficult to understand in C++. Therefore, the Java language is powerful and easy to use. As a representative of static object-oriented programming languages, the Java language implements object-oriented theory very well, allowing programmers to perform complex programming in an elegant way.

Java is characterized by simplicity, object-oriented, distributed, robustness, security, platform independence and portability, multi-threading, and dynamics. Java can write desktop applications, web applications, distributed systems, and embedded system applications.

JAVA implements a simple HTTP server

Set up a server to receive requests and return the correct response to the browser.

Project download address

Project goal: Implement a simple multi-threaded server that can handle requests from the browser (GET/POST) and make the correct response.

The request is divided into the following four types:

1. No parameters, text file type

2. No parameters, image file type

3. There are parameters, GET mode request, and complete form validation (login verification)

4. There are parameters, POST request, and form validation (login verification)

First of all, the basic implementation principle of this project should be clarified. The information requested by the user is read from the browser. The server parses and records the returned file name and parameter list. If the file exists, the file is read by the stream and returned to the browser. If it does not exist, return the corresponding prompt information. If the parameter list and the server store the same, return the login successfully, otherwise the return fails.

In the first step, since we want to parse the information transmitted from the browser , we must understand the components of the protocol HTTP\UDP\FTP and URL used to pass the information. Because it is a simple server, we only parse the HTTP protocol. .

How to implement a simple http server in java

Request refers to the request message from the client to the server.

The Request message is divided into three parts. The first part is called the request line, the second part is called http header, and the third part is the body. There is a blank line between the header and the body. The structure is as follows.

How to implement a simple http server in java

Method indicates the request method, such as "POST", "GET",

Path-to-resoure indicates the requested resource,

Http/version-number indicates the version number of the HTTP protocol.

When using the "GET" method, the body is empty. When using "POST", the body is not empty, but there is no line break, the readline() method cannot read

Response refers to the response from the server to the client.

The structure of the Request message is basically the same. It is also divided into three parts. The first part is called request line, the second part is called request header, and the third part is body. There is also a blank line between header and body. The structure is as follows.

How to implement a simple http server in java

The status code is used to tell the HTTP client whether the HTTP server has generated the expected Response. There are five types of status codes defined in HTTP/1.1, 1XX prompt information - indicating that the request has been successfully received, continue processing; 2XX success - indicating that the request has been Successful reception, understanding, acceptance; 3XX redirection - further processing is required to complete the request; 4XX client error - request has a syntax error or the request cannot be fulfilled; 5XX server side error - the server failed to implement a legitimate request, of course not Write is also available, the status code is convenient for the programmer to analyze whether the current page is correct or wrong.

In the second step, after understanding the URL and HTTP protocol, you can start building the project.

Current UML diagram for this project

How to implement a simple http server in java

The third step is to prepare text, images, HTML files, and then start editing.

Effect chart: (port number: 23333) 2333...

Default access

How to implement a simple http server in java

Aaron.txt

How to implement a simple http server in java

A.jpg

How to implement a simple http server in java

GET/POST request

Pay attention to the changes in the address bar

Login.html(GET)

How to implement a simple http server in java

Login.html(GET) (login failure)

How to implement a simple http server in java

Login.html(GET) (login success)

How to implement a simple http server in java

Login.html(POST)

How to implement a simple http server in java

Login.html(POST) (login failure)

How to implement a simple http server in java

Login.html(POST) (login success)

How to implement a simple http server in java

Trailer Type Diesel Generator

Trailer Type Diesel Generator,Trailer Diesel Generator,Trailer Diesel Genset,Mobile Diesel Geneator

Jiangsu Vantek Power Machinery Co., Ltd , https://www.vantekpower.com