/Users/lyon/j4p/src/net/web/WebServer.java

1    package net.web; 
2     
3    import java.io.FileReader; 
4    import java.io.IOException; 
5    import java.io.InputStreamReader; 
6     
7    public class WebServer { 
8      public static void main(String args[]) { 
9        WebServer ws = new WebServer(8080); 
10     } 
11    
12     public WebServer(int port) { 
13    
14         try { 
15             java.net.ServerSocket ss 
16                 = new java.net.ServerSocket(port); 
17             while (true) { 
18               startClient(ss.accept()); 
19             } 
20         } catch (IOException e) { 
21             e.printStackTrace(); 
22         } 
23    
24     } 
25    
26     public void startClient(java.net.Socket s) 
27         throws java.io.IOException { 
28       ClientThread 
29           ct = new ClientThread(s); 
30       Thread t = new Thread(ct); 
31       t.start(); 
32     } 
33   } 
34    
35   class ClientThread implements 
36       Runnable { 
37     private java.net.Socket s; 
38     private java.io.BufferedReader br; 
39     private java.io.PrintWriter pw; 
40     private static final String 
41         notFoundString = 
42         "HTTP/1.0 501 Not Implemented\n" 
43         + "Content-type: text/plain\n\n"; 
44     private static final String 
45         okString = 
46         "HTTP/1.0 200 OK\n" 
47         + "Content-type: text/html\n\n"; 
48    
49    
50     ClientThread(java.net.Socket _s) { 
51       s = _s; 
52       try { 
53         br = 
54             new java.io.BufferedReader( 
55                 new InputStreamReader( 
56                     s.getInputStream())); 
57         pw = 
58             new java.io.PrintWriter( 
59                 s.getOutputStream(), true); 
60       } catch (java.io.IOException e) { 
61         e.printStackTrace(); 
62       } 
63     } 
64    
65     public void run() { 
66       try { 
67         String line = 
68             br.readLine(); 
69         java.util.StringTokenizer 
70             st = 
71             new java.util.StringTokenizer( 
72                 line); 
73         System.out.println(line); 
74         if (st.nextToken().equals("GET")) 
75           countTo10(); 
76           //getAFile(st); 
77         else 
78           pw.println(notFoundString); 
79         pw.close(); 
80         s.close(); 
81       } catch (Exception e) { 
82       } 
83     } 
84    
85     public void countTo10() { 
86       pw.println("HTTP/1.0 200 OK"); 
87       pw.println("Content-type: text/plain"); 
88       pw.println(); 
89       for (int i = 0; i < 1000; i++) 
90         pw.println("i=" + i); 
91     } 
92    
93     public void getAFile(java.util.StringTokenizer st) 
94         throws java.io.FileNotFoundException, 
95         IOException { 
96       pw.println(okString); 
97       String fileName 
98           = st.nextToken(); 
99       pw.println("date=" + new java.util.Date()); 
100      java.io.BufferedReader 
101          fileReader = new 
102              java.io.BufferedReader(new 
103                  FileReader( 
104                      "c:\\lyon\\www" + fileName)); 
105      String fileLine = null; 
106      while ( 
107          (fileLine = 
108          fileReader.readLine()) 
109          != null) 
110        pw.println(fileLine); 
111    } 
112  } 
113