java how to handle http get request after establishing tcp connection

Solutions on MaxInterview for java how to handle http get request after establishing tcp connection by the best coders in the world

showing results for - "java how to handle http get request after establishing tcp connection"
María Camila
09 Aug 2017
1ServerSocket serverSock = new ServerSocket(6789);
2Socket sock = serverSock.accept();
3
4InputStream sis = sock.getInputStream();
5BufferedReader br = new BufferedReader(new InputStreamReader(sis));
6String request = br.readLine(); // Now you get GET index.html HTTP/1.1`
7String[] requestParam = request.split(" ");
8String path = requestParam[1];
9
10PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
11File file = new File(path);
12if (!file.exists()) {
13     out.write("HTTP 404"); // the file does not exists
14}
15FileReader fr = new FileReader(file);
16BufferedReader bfr = new BufferedReader(fr);
17String line;
18while ((line = bfr.readLine()) != null) {
19    out.write(line);
20}
21
22bfr.close();
23br.close();
24out.close();
25