Java Simulation Web server code

Source: Internet
Author: User

To better understand how a Web server, such as Tomcat, works, a Web server is now emulated with Java code:

1 ImportJava.io.*;2 Importjava.net.*;3  Public classMyWebServer4 {5     /*impersonate a Web server*/6      Public Static voidMain (string[] args)throwsException7     {8ServerSocket SS =NewServerSocket (9999);//occupies 9999 ports9          while(true){TenSocket s = ss.accept ();//Accept Request OneSystem.out.print ("9999 Port received request"); A             //process the request and return the request result -OutputStream out =S.getoutputstream (); -BufferedReader br =NewBufferedReader (NewFileReader ("d:\\servlet-learnning\\hello.html")); theString line =NULL; -              while((Line=br.readline ())! =NULL){ - Out.write (Line.getbytes ()); -             } + br.close (); - out.close (); +         } A     } at}

Note: This code simulates a Web server that occupies a 9999 port and returns the d:\servlet-learnning\hello.html to the browser after it receives the request.

Compile and run this code, and then in the browser with http://localhost:9999 access to the emulated server, return the following results:

Note: The page displays the contents of the result as d:\servlet-learnning\hello.html

Java Simulation Web server code

Related Article

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.