您的位置:首页 > 理论基础 > 计算机网络

[Tomcat服务器 --- 一个简单的基于HTTP WEB服务器]

2013-07-22 21:14 645 查看
package com.tomcat.startup;import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;import com.tomcat.http.Request;
import com.tomcat.http.Response;/**
* WEB服务器的启动类
* @author Administrator
*
*/
public class WebServer {
/**将资源指定到项目的某个目录下*/
public final static String WEB_ROOT = System.getProperty("user.dir") + File.separator + "WebRoot";
/**指定关闭服务器的指令,默认是不关闭的*/
private static boolean shutdown = false;

/**启动主方法*/
public static void main(String[] args) {
/**创建WebServer实例*/
WebServer server = new WebServer();
server.start();
}

/**启动方法*/
public void start() {
/**
* 在启动之后,实际上就是创建Socket的过程,使用流来进行交流
*/
ServerSocket server = null;
try {
/**创建服务器Socket**/
server = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
/**若发生异常,则退出*/
System.exit(1);
}
/**循环等待request请求**/
while(!shutdown){//服务器没有关闭,即处于接收状态
/**获取客服端的请求,返回Socket,代表客服端的请求信息*/
Socket socket = null;
OutputStream out = null;mcat
InputStream is = null;
try {
/**使用accept接收请求*/
socket = server.accept();
out = socket.getOutputStream();//用于回复请求流
is = socket.getInputStream();//获取请求信息流
/**创建request对象**/
Request request = new Request(is);
request.parse();
/**创建response对象**/
Response response = new Response(out);
response.setRequest(request);
response.sendStaticResources();

socket.close();

shutdown = request.getUri().equalsIgnoreCase("shutdown");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

package com.tomcat.http;import java.io.IOException;
import java.io.InputStream;/**
* 代表客服端的请求(Request)
* @author Administrator
*
*/
public class Request {
/**请求端的数据流**/
private InputStream input;
/**uri:客服端请求的信息**/
private String uri;

/**构造Request对象**/
public Request(InputStream input) {
this.input = input;
}

/**解析request请求信息**/
public void parse() {
StringBuilder request = new StringBuilder(2048);
/**将信息读到buffer中**/
byte[] buffer = new byte[2048];
int i = 0;
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.println(request.toString());

/**获取URI**/
uri = parseUri(request.toString());
}
/**
* 根据请求信息,解析出uri
* @return
*/
public String parseUri(String request) {
/**第一个空格的位置**/
int index1 = 0;
/**第二个空格的位置**/
int index2 = 0;
index1 = request.indexOf(' ');//找到都一个空格的位置
if (index1 != -1) {
index2 = request.indexOf(' ',index1+1);//越过第1个位置,找第二个位置空格
if (index2 > index1) {
return request.substring(index1+1,index2);//取出uri
}
}
return null;
}

public String getUri() {
return uri;
}
}

package com.tomcat.http;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;import com.tomcat.startup.WebServer;
/**
* 响应请求
* @author Administrator
*
*/
public class Response {
private final static int BUFFER_SIZE = 1024;
private Request request;
/**用于写数据,返回给客服端**/
private OutputStream out;

/**构造方法**/
public Response(OutputStream out) {
this.out = out;
}
/**构造request对象**/
public void setRequest(Request request) {
this.request = request;
}

/**根据请求信息,响应处理**/
public void sendStaticResources() {
byte[] bytes = new byte[BUFFER_SIZE];
/**用该流,去读取请求的数据**/
FileInputStream fis = null;

/**根据uri地址,发送请求的信息**/
File file = new File(WebServer.WEB_ROOT,request.getUri());
/**判断请求的数据是否存在**/
try {
if (file.exists()) {//存在
fis = new FileInputStream(file);//创建流,用于响应操作
int index = fis.read(bytes,0,BUFFER_SIZE);
if(index != -1) {
out.write(bytes,0,index);
}
}else {//不存在
String errorMessage = "HTTP/1.1 404 File not found\r\n"
+ "Content-Type: text/html\r\n"
+ "Content-Length: 23\r\n"
+ "\r\n"
+ "<h1>file not found</h1>";
out.write(errorMessage.getBytes());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息