I learn the code from the first example of .
here is the index.html
<html>
<head>
<title>Welcome to BrainySoftware</title>
</head>
<body>
<img src="./images/logo.gif">
<br>
Welcome to BrainySoftware.
</body>
</html>
I know there will be 2 GET request. First is the html,the second is the img. But I do receive the third request sometimes. The third request is empty, I can get nothing from the request.
ServerSocket serverSocket = null;
int port = 8080;
try {
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
while (!shutdown) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream();
// create Request object and parse
Request request = new Request(input);
request.parse();
// create Response object
Response response = new Response(output);
response.setRequest(request);
response.sendStaticResource();
// Close the socket
socket.close();
//check if the previous URI is a shutdown command
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
}
catch (Exception e) {
e.printStackTrace();
continue;
}
}
When the third request comes, the method sendStaticResource
throw an exception:
java.lang.NullPointerException because request.getUri() is null.
My jdk version is 1.7,when I change the InetAddress from 127.0.0.1
to 192.168.50.132
,the third request disappeared.
I have no idea why not 2 requests, please help me.