Java EE (i)--Developing a simple Web server

Source: Internet
Author: User

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

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.