《How Tomcat Works》学习笔记(一)

这本写的很好,在学完java 网络编程后,感觉能够将所学的知识用上。

following for each HTTP request for a servlet:

          1.When the servlet is called for the first time, load the servlet class and call the servlet's init method (once only)

   2.For each request, construct an instance of javax.servlet.ServletRequest and an instance of   javax.servlet.ServletResponse.

    3.Invoke the servlet's service method, passing the ServletRequest and ServletResponse objects.

4.When the servlet class is shut down, call the servlet's destroy method and unload the servlet class.

下面是HTTP请求的过程:

1.Wait for HTTP requests.

2.Construct a ServletRequest object and a ServletResponse object.

    3.If the request is for a static resource, invoke the process method of the StaticResourceProcessor instance, passing the ServletRequest and ServletResponse objects.

4.If the request is for a servlet, load the servlet class and invoke the service method of the servlet, passing the  ServletRequest and ServletResponse objects.

根据书上的第二章写了一个简单的服务器:

目录结构:


package com.test.server;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServer {
	
	public static final String WEB_CONTENT = System.getProperty("user.dir")+File.separator+"WebContent";
	
	public static final String SHUTDOWN = "/SHUTDOWN";

	private static boolean  shutdown = false;
	public static void main(String[] args) {
		
	  new HttpServer().await();	
	}
	public void await()
	{
		ServerSocket serverSocket  = null;
		try 
		{
		serverSocket = new ServerSocket(8080, 1, InetAddress.getByName("127.0.0.1"));
		
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		while(!shutdown)
		{
			Socket socket = null;
			
			InputStream is = null;
			
			OutputStream os = null;
			try
			{
				socket = serverSocket.accept();
				
				is = socket.getInputStream();
				
				os = socket.getOutputStream();
				
				
				Request request = new Request(is);

				Response response = new Response(os);
				
				request.parse();
				
				response.setRequest(request);
				
				response.sendStaticResource();
				
				shutdown = request.getUri().equals(SHUTDOWN);
				
			}catch(Exception e)
			{
				e.printStackTrace();
				continue;
			}
		}
		
	}
}


Request:

package com.test.server;

import java.io.InputStream;

public class Request {

	private InputStream is;
	
	private String uri;
	
	public Request(InputStream is) 
	{
		this.is = is;
	}
	
	public String getUri()
	{
		return this.uri;
	}
	//解析请求
	public void  parse()
	{
		System.out.println("parse...........");
		StringBuffer sb = new StringBuffer(2048);
		int i;
		byte[] buff = new byte[2048];
		try
		{
			
		    i = is.read(buff);
		}catch(Exception e)
		{
			e.printStackTrace();
			i = -1;
		}
		for(int j=0; j<i; j++)
		{
			sb.append((char)buff[j]);
		}
		
		System.out.println(sb.toString());
		
		uri = parseUri(sb.toString());
		System.out.println("uri: "+uri);
	}
	//解析请求页面
	private String parseUri(String source)
	{
		int index1, index2;
		index1 = source.indexOf(" ");
		if(-1 != index1)
		{
			index2 = source.indexOf(" ", index1+1);
			if(index2>index1)
			{
				return source.substring(index1+1, index2);
			}
		}
		return null;
	}
	
}

Response:

package com.test.server;

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

public class Response {

	private static final int BUFF_SIZE = 1024;
	
	private OutputStream os;
	
	private Request request;
	
	public Response(OutputStream os)
	{
		this.os = os;
	}
	public void setRequest(Request request)
	{
		this.request = request;
	}
	
	public void sendStaticResource() throws IOException
	{
		byte[] buff = new byte[BUFF_SIZE];
		FileInputStream fis = null;
		try
		{
			File file =new File(HttpServer.WEB_CONTENT, request.getUri());
			if(file.exists())
			{
				fis = new FileInputStream(file);
				
				int length = -1;
				while(-1 != (length = fis.read(buff)))
				{
					os.write(buff, 0, length);
				}
			}else
			{
				System.out.println("Not File");
				// file not found
				String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n" +"\r\n" +"File Not Found";
			    os.write(errorMessage.getBytes());
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(null!= fis)
			{
				fis.close();
			}
		}
		
	}
}


Hello.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
		<h1>Hello World</h1>
</body>
</html>

在浏览器上访问效果如下:






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值