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 ();}}