I. Introduction ofWeb development
Web Connection process ( b/s): The client uses the browser to send an HTTP request to the Web server and the server responds. Browser/server
HTTP request content:
The get/http/1.1 description is a GET request, using the http1.1 protocol.
Accept:image/gif ..... Description I can receive the image format has the following ...
ACCEPT-LANGUAGE:ZH-CN. It means that the language I can receive is Chinese.
User-agent:xxx shows that I can support the browser has XXX.
Host:www.baidu.com indicates that the host I want to access is www.baidu.com
Web Connection process ( c/s): The client (the client writes its own program) uses the TCP/IP protocol to send the request to the server and the server responds. Client/server
TCP/IP requests: In fact, the HTTP request is a wrapped TCP/IP request.
Ii. simulating the development of a Web server
1. First write an HTML file for display (placed in e:\\webtest/hello.html)
2. Write a mywebserver.java as follows:
import Java.io.BufferedReader;
import Java.io.FileReader;
import Java.io.OutputStream;
import Java.net.ServerSocket;
import Java.net.Socket;
Public class MyWebServer {
Public Static void Main (string[] args) throws exception{
TODO Auto-generated method stubs
ServerSocket ss=New serversocket (9999); Set port number 9999
System. out. println ("Wait for connection on port 9999 ..."); Wait for a request to send over
Socket s=ss.accept ();
System. out. println ("Successfully connected on port 9999 ...");
OutputStream Os=s.getoutputstream ();
BufferedReader br=New BufferedReader (new FileReader ("e:\\webtest/hello.html"));
String bfstring= "";
while ((Bfstring=br.readline ()) =null) {
Os.write (Bfstring.getbytes ());
}
Br.close ();
Os.close ();
S.close ();
}
}
3. After compiling and running this Java file, the browser enters http://localhost:9999/to display the page that was first written.
4. It is worth mentioning that if Mywebserver.java writes 80 ports, it can be accessed directly http://localhost.
5. Also, if you want multiple people to access this page at the same time, you need to change the code to
import Java.io.BufferedReader;
import Java.io.FileReader;
import Java.io.OutputStream;
import Java.net.ServerSocket;
import Java.net.Socket;
Public class MyWebServer {
Public Static void Main (string[] args) throws exception{
TODO Auto-generated method stubs
ServerSocket ss=New serversocket (9999); Set port number 9999
while (true) {
System. out. println ("Wait for connection on port 9999 ..."); Wait for a request to send over
Socket s=ss.accept ();
System. out. println ("Successfully connected on port 9999 ...");
OutputStream Os=s.getoutputstream ();
BufferedReader br=New BufferedReader (new FileReader ("e:\\webtest/hello.html"));
String bfstring= "";
while ((Bfstring=br.readline ()) =null) {
Os.write (Bfstring.getbytes ());
}
}
Br.close ();
Os.close ();
S.close ();
}
}
Java EE (i)--Developing a simple Web server