实现简单版的Tomcat

Tomcat实现原理:

在这里插入图片描述

案例1:

1、用户浏览器访问服务器,服务器获取用户信息并打印用户信息和自身信息
2、服务器将服务器内存中的文件发送给客户浏览器

copyTom 实现Runnable接口
copyTom的run方法中实现服务器的功能
//while((line=br.readLine())!=null && line.length() > 0){
//保证写入的line长度》0 不然页面加载不出来

访问:http://192.168.1.170/index.html (先开启服务器)

import java.io.*;
import java.net.Socket;
public class CopyTom implements Runnable{	
	private Socket s;	
	public CopyTom(Socket s) {
		super();
		this.s = s;
	}
	//请求套路:一定会从客户端带点东西给服务器,服务器也一定会有东西返回!
	//接收客户端的请求(获取客户从客户端带过来的数据)
	//在控制台打印给客户端浏览器的web资源  
	//while((line=br.readLine())!=null && line.length() > 0){
	//保证写入的line长度》0  不然页面加载不出来
	
	public void run() {
		// TODO Auto-generated method stub
		PrintWriter pw=null;
		OutputStream os=null;
		try {
			//取得请求头数据
			InputStream is = s.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			StringBuffer sb=new StringBuffer();
			String line=null;
			while((line=br.readLine())!=null && line.length() > 0){//保证写入的line长度》0  不然页面加载不出来
				sb.append(line);
				sb.append("\r\n");
			}
			System.out.println(sb);			
			//给客户浏览器响应
			String webRoot="E:/代码web/期末项目";
			String []messages=sb.toString().split(" ");
			System.out.println(messages);
			os=s.getOutputStream();//准备好Socket写入
			pw=new PrintWriter(os);//用printwriter包装下(printWriter啥都能放!!!!)
			pw.println("HTTP/1.1 200 OK");//转成http的格式
			pw.println("Content-type: */*;charset=utf-8");
			pw.println();
			pw.flush();
			System.out.println(webRoot+messages[1]);
			if(messages.length>2){//准备写入
				File f=new File(webRoot+messages[1]);//在对应文件夹下获取:messages【1】就是对应的文件名
				FileInputStream fis=new FileInputStream(f);
				byte[] bs=new byte[1024];
				int len=0;
				while((len=fis.read(bs))!=-1){
					os.write(bs);
					os.flush();
				}
				fis.close();
			}		
			System.out.println("wancheng !!!!");					
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				//打印错误页面
				FileInputStream fs=new FileInputStream("E:/代码web/期末项目/后台登录.html");
				byte []bs=new byte[1024];
			int len =0;
			while((len=fs.read(bs))!=-1){
				os.write(bs);
				os.flush();
			}
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}			
		}
	}	
}

messages【1】就是对应的文件名:/XXX.html
接收客户端的请求(获取客户从客户端带过来的数据)
在控制台打印给客户端浏览器的web资源

在这里插入图片描述
项目启动:

public class TomcatTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//char ch='\65';
		ServerSocket ss=null;
		try {
			ss=new ServerSocket(80);
			System.out.println("项目启动成功");
			while(true){
				Socket s=ss.accept();//接收请求
				new Thread(new CopyTom(s)).start();			
			}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//默认端口
		finally{
			if(ss!=null){
				try{
					ss.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}

}

2服务端给客户端发送一张图片,图片发送接收在子线程中完成

/*
 * 14.	写一个Socket程序,完成服务端给客户端发送一张图片(图片发送接收在子线程中完成)
 */
public class T_14 {
	public static void main(String[] args) {
		new Thread(new  Runnable() {
			public void run() {
				send();
			}
			public void send(){
				try {
					Socket s = new Socket("192.168.1.170",4518);
					BufferedInputStream bi = new BufferedInputStream(new FileInputStream("ax.jpg"));
					BufferedOutputStream bo = new BufferedOutputStream(s.getOutputStream());
					int len=0;
					byte[] bs=new byte[1024];
					while((len=bi.read(bs))!=-1){
						bo.write(bs);
						bo.flush();
					}
					s.shutdownOutput();
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				get();
			}
			
			public void get(){
			
				String filename="a/"+System.currentTimeMillis()+"A";
				try (ServerSocket ss=new ServerSocket(4518);
						Socket s = ss.accept();
						BufferedInputStream bi=new BufferedInputStream(s.getInputStream());
					BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(filename+".jpg"));
						){
					
					int len =0;
					byte[] bs=new byte[1024];
					while((len=bi.read(bs))!=-1){
						bo.write(bs);
						bo.flush();
					}
					s.shutdownInput();
						} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}).start();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值