Java Web 服务器原理

写下这编文章只是为了让自己不忘记以前学过的知识,下面是基于Socket来完成浏览器的访问,就类似于tomcat(不过tomcat比我功能更全更强大),那下面来看代码:

很简单 先由 HttpServer 来接受浏览器的问访问之后就交给HttpThread类来响应。


package com.zx;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author zhouxiang
* @version Feb 21, 2013 10:22:55 AM
*/
public class HttpServer {
// 默认ROOT文件夹
public static String ROOT = "./wwwroot";
// 默认文件的文件名
public static String defaultPage = "index.html";

public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8000);
while(true){
// 阻塞,等待浏览器的连接
Socket socket = server.accept();
System.out.println("Accept Connection.....\n");
// 启动服务线程
new HttpThread(socket).start();
}
}
}



HttpThread获取到io流并作出响应

package com.zx;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
* @author zhouxiang
* @version Feb 21, 2013 10:33:52 AM
*/
public class HttpThread extends Thread {

private Socket socket;

public HttpThread(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = this.socket.getInputStream();
outputStream = this.socket.getOutputStream();
Receiver receiver = new Receiver(inputStream);
// 取得浏览器发过来的URL请求
String sURL = receiver.parese();
if (sURL.equals("/")) {
sURL = HttpServer.defaultPage;
}
Answer ans = new Answer(outputStream);
ans.send(sURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
if(socket != null ){
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}



Receiver这个类获主要取浏览器访问的地址

package com.zx;

import java.io.IOException;
import java.io.InputStream;

/**
* @author zhouxiang
* @version Feb 21, 2013 10:40:39 AM
*/
public class Receiver {

InputStream inputStream = null;
public Receiver(InputStream inputStream) {
this.inputStream = inputStream;
}

/**
* 这个方法的目的是将URL请求的文件返回
* @return
*/
public String parese(){
StringBuffer receiveStr = new StringBuffer(2048);
int i = 0;
byte[] bytes = new byte[2048];
try {
//从socket读出数据
i = inputStream.read(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
// 读出空数据
System.out.println("读取流异常......");
i = -1;
}
System.out.println("读取的字节长度:"+""+i);
for(int j = 0 ; j < i ; j++){
// 读出数据以字符方式保存到receiveStr
receiveStr.append((char)bytes[j]);
}
return getUri(receiveStr.toString());
}

/**
* 将收到的HTTP协议数据包分解,取出文件名
* @param receiveStr
* @return
*/
private String getUri(String receiveStr) {
int index1,index2;
index1 = receiveStr.indexOf(' ');
if(index1 != -1){
index2 = receiveStr.indexOf(' ',index1 + 1);
if(index2 > index1){
return receiveStr.substring(index1 + 1, index2);
}
}
return null;
}

}



Answer这个就是响应类了

package com.zx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
* @author zhouxiang
* @version Feb 21, 2013 11:54:10 AM
*/
public class Answer {

OutputStream out = null;

public Answer(OutputStream outputStream) {
this.out = outputStream;
}

public void send(String pageFile) {
// TODO Auto-generated method stub
byte[] bytes = new byte[2048];
FileInputStream fis = null;
try {
File file = new File(HttpServer.ROOT, pageFile);
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, 2048);
String sBody = new String(bytes);
String sendMessage = "HTTP/1.1 200 OK\r\n"+"Content-Type: text/html\r\n"+"Content-Length: "+ch+"\r\n"+
"\r\n"+sBody;
out.write(sendMessage.getBytes());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}



代码很简单主要就是了解下,刚学习服务器的同学现在应该不会对服务器有什么距离了吧!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值