《分布式计算原理及应用》实验报告
题目: Web服务器的实现
学生姓名:
班级:软件工程1202
学号:
指导老师:张庆生
成绩:
西安邮电大学计算机学院
2015 年 4 月 10日
一、 实验目的
①掌握如何创建监听端口请求
②了解Http的GET请求命令格式
③了解GET请求的实现机制
二、 实验内容及要求
①连接:Web浏览器与Web服务器建立连接,打开一个socket套接字,建 立连接。
②Web浏览器通过socket向Web服务器提交请求。HTTP的请求以GET方法。
③Web浏览器提交请求后,通过HTTP协议传送给Web服务器,Web服务器 接收到之后,处理请求,并将处理结果通过HTTP传给Web浏览器,将请 求的内容在web浏览器上显示出来.
三、 实验过程
1、 创建监听服务的端口;
核心代码:当程序运行之后开始监听8080端口是是否有请求:
package http;
import .Socket;
import .ServerSocket;
import .InetAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;
public class HttpServer {
public static final String WEB_ROOT =
System.getProperty("user.dir") + File.separator + "webroot";
// shutdown command
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
// the shutdown command received
private boolean shutdown = false;
public static void main(String[] args) {
HttpServer server = new HttpServer();
server.await();
}
public void await() {
ServerSocket serverSocket = null;
int port = 8091;
try {
serverSocket = new ServerSocket(port, 1,
InetAddress.getByName("127.0.0.1"));
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// Loop waiting for a request
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;
}
}
}
}
2、 当接收到请求之后,开启线程处理请求;
package http;
import java.io.InputStream;
import java.io.IOException;
public class Request {
private InputStream input;
private String uri;
public Request(InputStream input) {
new InputtoFilr().saveinFile();
this.input = input;
}
public void parse() {
// Read a set of characters from the socket
StringBuffer request = new StringBuffer(2048);
int i;
byte[] buffer = new byte[2048];
try {
i = input.read(buffer);
}
catch (IOException e) {
e.printStackTrace();
i = -1;
}
for (int j=0; j<i; j++) {
request.append((char) buffer[j]);
}
System.out.print(request.toString());
uri = parseUri(request.toString());
}
private String parseUri(String requestString) {
int index1, index2;
index1 = requestString.indexOf(' ');
if (index1 != -1) {
index2 = requestString.indexOf(' ', index1 + 1);
if (index2 > index1)
return requestString.substring(index1 + 1, index2);
}
return null;
}
public String getUri() {
return uri;
}
}
3、 编写解析请求的功能程序
package http;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;
public class Response {
private static final int BUFFER_SIZE = 1024;
Request request;
OutputStream output;
public Response(OutputStream output) {
this.output = output;
}
public void setRequest(Request request) {