A simple web server written in Java (suitable for beginners)

Source: Internet
Author: User

A Web written in Java has the following effect:

1. The port is in use. You can try another port;

 

2. There are too many types of software installed on some computers, and sometimes they cannot receive ports. If they are correct, don't worry about it on a computer. I am suffering from it;

 

 

3. To ensure the effectiveness of the development process, continuous testing is required, and testing is very important. If you write a lot in one breath without testing, a small error may make you confused for a long time;

 

The following code is used:

 

 

Create a package named webroot in your project, and then create

 

The following code is missing from an object named index.html:

 

<HTML> 

Next is your class code:

 

/*****/Package web. server; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. printstream; import java.net. serversocket; import java.net. socket;/*** @ author administrator **/public class webserver {Private Static final int post = 8124; Private Static final string uri = system. clearproperty ("user. dir ") + file. separator + "webroot" + file. separator;/*** @ Param RGS */Public webserver () {system. out. println ("webserver () ------ Star"); system. out. println ("url ------------------->" + URI); system. out. println ("file --------------->" + file. separator); try {serversocket listener = new serversocket (post); system. out. println ("listener --- the server is running port --------------->" + listener. getlocalport (); While (true) {system. out. println ("determine whether to enter the loop"); Socket socket = listener. accept (); SY Stem. Out. println ("socket ------------------->" + socket. getlocalport (); // new printwriter (socket. getoutputstream (), true). println ("Hello! World !! "); // Bufferedreader in = new bufferedreader (New inputstreamreader (socket. getinputstream (); // system. out. println ("in --------------------------->" + in); // output stream printstream out = new printstream (socket. getoutputstream (), true); // set the path file fileuri = new file (URI + "index.html"); system. out. println ("fileuri ---------------------------->" + fileuri); // set the file type string contenttype = NULL; contenttype = "text/html; c Harset = GBK "; if (! Fileuri. exists () {// if this file does not exist. out. println ("this file is not found");} else {system. out. println ("find this file");/* out. println ("HTTP/1.0 200 OK"); out. println (contenttype); out. println (); */} fileinputstream FCM = NULL; FD = new fileinputstream (fileuri); system. out. println ("FS ------------------------->" + fiis); byte data []; Data = new byte [fiis. available ()]; system. out. println ("Data ------------------>" + data); FCM. rea D (data); Out. Write (data); Out. Close (); If (FS! = NULL) {FS. close ();} socket. close () ;}} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}/ * Public void run () {system. out. println ("Run () ------------ start");} */public static void main (string [] ARGs) {// todo auto-generated method stub new webserver ();}}

 

 

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.