模拟tomcat工作原理

11 篇文章 0 订阅
1 篇文章 0 订阅
本文也是参考了网上的资料,加上我自己的整理得出的。

httpserver原理:服务器端 打开一个socket,一直处在监听tomcat指定的 ip 的指定端口,一旦有访问的,就开启一个线程去处理,代码如下:

-------------------------------server:

package com.kaobian;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import com.session.HttpSession;

public class HttpServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
while (true) {
Socket socket = server.accept();
HttpSession session = new HttpSession(socket);
new Thread(session).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

---------------------------------处理线程

package com.session;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import com.config.Config;

public class HttpSession implements Runnable {
private String path = Config.getConfig().getPath();
private Socket socket ;

public HttpSession(Socket socket){
super();
this.socket = socket;
}

public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
OutputStream out = this.socket.getOutputStream();
String command = null;
while((command = br.readLine()) != null){
System.out.println("浏览器的指令:"+command);
if(command.length() <3){
break;
}
String result = command.substring(0,3);
if(result.equalsIgnoreCase("GET")){
int begin = command.indexOf("/")+1;
int end = command.lastIndexOf(" ");
String fileName = command.substring(begin,end);
doGet(fileName,out);
}

}
out.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private void doGet(String fileName, OutputStream out) {
File file = new File(path+fileName);
if(!file.isDirectory()){
if(!file.exists()){
file = new File(path + Config.getConfig().getDefaultPage());
}
}else {
file = new File(path + Config.getConfig().getDefaultPage());
}
InputStream is = null;
try {
is = new FileInputStream(file);
byte[] data =new byte[1024];
int len = 0;
while((len = is.read(data)) != -1){
out.write(data,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
is = null;
}
}
}

}

}

---------------------------读取配置文件

package com.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Config {

private String path ;
private String defaultPage;
private static Config config ;

public static Config getConfig(){
if(config == null){
config = new Config();
config.init();
}
return config;
}

private void init(){
Properties properties = new Properties();
try {
InputStream reader = new FileInputStream(new File("config.properties"));
properties.load(reader);
this.path = properties.getProperty("path");
this.defaultPage = properties.getProperty("defaultIndex");
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private Config(){
}

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDefaultPage() {
return defaultPage;
}
public void setDefaultPage(String defaultPage) {
this.defaultPage = defaultPage;
}

}

------------------配置文件要放到工程的根目录下,要不然自行修改属性文件的位置

path=c://
defaultIndex=div.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值