/Users/lyon/j4p/src/net/rmi/rmiSynth/WebServer.java

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