A Simple Servlet Container

来源:互联网 发布:itoa函数在linux 编辑:程序博客网 时间:2024/06/11 07:25

I will develop my own servlet container by presenting two applications.
The first application has been designed to be as simple as possible to make it easy for you to understand how a servlet container works.It then evolves into the second servlet container,which is slightly more complex.

Application 1
Now, let’s examine servlet programming from a servlet container’s perspective. In a nutshell, a fully-functional servlet container does the following for each HTTP request for a servlet:
When the servlet is called for the first time, load the servlet calss and call the servlet’s init method(only once)
For each request ,construct an instance of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse.
Invoke the servlet’s service method, passing the ServletRequest and ServletResponse objects.
When the servlet class is shut down, call the servlet’s destory method and unload the servlet class.

The first servlet container for this chapter is not fully functional. Therefore, it cannot run other than very simple servlets and does not call the servlet’s init and destory methods. Instead, it does the following:
Wait forr HTTP requests.
Construct a ServletRequest object and a ServletResponse object.
If the request is for a static resource, invoke the process method of the StaticResourceProcessor instance, passing the ServletRequest and ServletResponse objects.
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.

Note In this servlet container, the servlet class is loaded every time the servlet is requested.

The first application consists of six classes:
1 HttpServer1
2 Request
3 Response
4 StaticResourceProcessor
5 ServletProcessor1
6 Constants

0 0
原创粉丝点击