Create a basic HTTP server in Java that answers browser requests and serves the requested page to the client, using the ServerSocket object.
1.import java.io.BufferedReader;
2.import java.io.File;
3.import java.io.FileReader;
4.import java.io.IOException;
5.import java.io.InputStreamReader;
6.import java.io.PrintWriter;
7.import java.net.ServerSocket;
8.import java.net.Socket;
9.import java.util.StringTokenizer;
10.
11.public class BasicServer
12.{
13. public static void main(String[] args)
14. {
15. final int SERVER_PORT = 1986;
16. ServerSocket serverSocket = null;
17. String servPath = "src/pociu/cis201/server/wwwroot/";
18.
19. try
20. {
21. serverSocket = new ServerSocket(SERVER_PORT);
22. }
23. catch (IOException e)
24. {
25. System.err.println("Could not listen on port: " + SERVER_PORT);
26. System.exit(1);
27. }
28.
29. while (true)
30. {
31. Socket clientSocket = null;
32. try
33. {
34. clientSocket = serverSocket.accept();
35. }
36. catch (IOException e)
37. {
38. System.err.println("Accept failed.");
39. System.exit(1);
40. }
41. PrintWriter out=null;
42. BufferedReader in=null;
43. try
44. {
45. out = new PrintWriter(clientSocket.getOutputStream(), true);
46. in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
47. out.flush();
48.
49. String s;
50. while (!(s = in.readLine()).isEmpty())
51. {
52. if(s.length() > 0)
53. {
54. StringTokenizer tokens = new StringTokenizer(s);
55.
56. // Unfortunately Java won't do Switch on strings...
57. if(tokens.nextToken().equals("GET"))
58. {
59. String fileRequested = tokens.nextToken().replace("/", "");
60. //Remove the query string before attempting to retrieve the file from the file system
61. fileRequested = RemoveQString(fileRequested);
62. if(fileRequested.equals("")) // If the root is requested
63. {
64. fileRequested = "index.html"; // Show the index.html page
65. }
66. File reqFile = new File(servPath + fileRequested);
67. System.out.println("File " + fileRequested + " was requested.");
68. if(reqFile.exists())
69. {
70. try
71. {
72. FileReader fText = new FileReader(reqFile);
73. BufferedReader bfFile = new BufferedReader(fText);
74.
75. String line;
76. while ((line = bfFile.readLine()) != null)
77. {
78. out.write(line);
79. }
80. out.flush();
81. bfFile.close();
82. System.out.println("The file was served.");
83. }
84. catch(IOException e)
85. {
86. out.write("The file you have requested (" + fileRequested + ") could not be served: " + e.getMessage());
87. out.flush();
88. System.out.println("An IO exception occured.");
89. }
90. }
91. else
92. {
93. out.write("The file you have requested (" + fileRequested + ") was not found.");
94. out.flush();
95. System.out.println("File not found.");
96. }
97. }
98. }
99. }
100. out.close();
101. in.close();
102. clientSocket.close();
103. }
104. catch (IOException e)
105. {
106. e.printStackTrace();
107. }
108. }
109. }
110.
111. private static String RemoveQString(String str)
112. {
113. int qPos = str.indexOf("?");
114. if(qPos >= 0)
115. {
116. str = str.substring(0, qPos);
117. }
118. return str;
119. }
120.}