欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

模拟Servlet容器管理Servlet

程序员文章站 2022-04-04 16:40:15
实现Servlet容器(Servlet容器根据web.xml文件调用对应的Servlet类) Servlet(狭义解释是Servlet接口,但更常见的解释是实现Servlet的所有类) 介绍:Servlet容器是用来管理Servlet, 过程:浏览器输入URL地址,服务器接收到这个地址,根据浏览器的 ......

实现Servlet容器(Servlet容器根据web.xml文件调用对应的Servlet类)

Servlet(狭义解释是Servlet接口,但更常见的解释是实现Servlet的所有类)

模拟Servlet容器管理Servlet

介绍:Servlet容器是用来管理Servlet,

过程:浏览器输入URL地址,服务器接收到这个地址,根据浏览器的访问方式,响应消息发送给浏览器,并在浏览器上显示。

Servlet的工作过程:

1.浏览器提交请求(通常是URL或超链接或表单提交)

2.服务器接收到请求后,判断请求的Servlet如果是第一次请求就实例化一个Servlet

3.调用Servlet的service()方法

4.服务器把产生的响应内容发送给客户端

5.浏览器根据自己的解析方式对响应内容进行解析

代码实现:

编写各类的功能:

HttpServletRequest解析URL,得到请求方式和地址等信息

HttpServletResponse封装Http响应消息

HttpServlet抽象类,doService()方法根据请求的方式执行相应的子类方法

Service类有主函数,服务器Socket设置端口号,接收客户端的socket对象,这里读取一个属性文件web.properties来模拟配置文件Web.xml,实现URL到类的映射

MyServlet和MyServlet2继承自HttpServlet,相应的方法把html通过httpServletResponse发送给浏览器

模拟Servlet容器管理Servlet
  1 package com.wj;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.net.Socket;
  7 
  8 /*
  9  * 解析浏览器的请求,得到请求的方式和地址等信息
 10  */
 11 public class HttpServletRequest {
 12 
 13     private Socket socket;
 14     private String[] items;
 15     public HttpServletRequest(Socket socket) throws IOException {
 16         this.socket = socket;
 17         BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
 18         String cmd=br.readLine();
 19         items=cmd.split(" ");
 20         
 21     }
 22     public String getMethod(){
 23         return items[0];
 24     }
 25     public String getURI(){
 26         return items[1];
 27     }
 28     
 29 }
 30 package com.wj;
 31 
 32 import java.io.IOException;
 33 import java.io.OutputStream;
 34 import java.io.PrintWriter;
 35 import java.net.Socket;
 36 
 37 public class HttpServletResponse {
 38 
 39     private Socket socket;
 40     public HttpServletResponse(Socket socket){
 41         this.socket=socket;
 42     }
 43     public OutputStream getOutputStream() throws IOException{
 44         return socket.getOutputStream();
 45     }
 46     public PrintWriter getWriter() throws IOException{
 47         return new PrintWriter(this.socket.getOutputStream());
 48     }
 49 }
 50 package com.wj;
 51 
 52 import java.io.IOException;
 53 
 54 
 55 public abstract class HttpServlet{
 56 
 57 
 58     public abstract void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException;
 59 
 60     public abstract void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException;
 61 
 62     public void doService(HttpServletRequest req, HttpServletResponse res)
 63             throws IOException {
 64         String me=req.getMethod();
 65         if(me.equalsIgnoreCase("get")){
 66             doGet(req, res);
 67         }else{
 68             doPost(req, res);
 69         }
 70     }
 71 }
 72 package com.wj;
 73 
 74 import java.io.BufferedReader;
 75 import java.io.FileInputStream;
 76 import java.io.IOException;
 77 import java.net.ServerSocket;
 78 import java.net.Socket;
 79 import java.util.Properties;
 80 
 81 public class Service {
 82     public static void main(String[] args) throws IOException {
 83         // TODO Auto-generated method stub
 84         ServerSocket server = null;
 85         BufferedReader br = null;
 86 
 87         server = new ServerSocket(11111);
 88         Socket socket = null;
 89         while (true) {// loop
 90             try {
 91                 socket = server.accept();
 92                 HttpServletRequest request = new HttpServletRequest(socket);
 93                 HttpServletResponse response = new HttpServletResponse(socket);
 94                 Properties prop = new Properties();
 95                 prop.load(new FileInputStream("src/web.properties"));  //Java读取配置文件信息
 96                 String className = prop.getProperty(request.getURI());
 97                 HttpServlet servlet = (HttpServlet)Class.forName(className).newInstance();
 98                 servlet.doService(request, response);
 99             } catch (Exception ex) {
100                 System.out.println(ex.getMessage());
101             } finally {
102                 if(socket != null){
103                     socket.close();
104                 }
105             }
106         }
107     }
108 
109 }
110 package com.wj2;
111 
112 import java.io.IOException;
113 import java.io.PrintWriter;
114 
115 import com.wj.HttpServlet;
116 import com.wj.HttpServletRequest;
117 import com.wj.HttpServletResponse;
118 
119 public class MyServlet extends HttpServlet{
120 
121     @Override
122     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
123         // TODO Auto-generated method stub
124         String html = "<html><head><title>hello</title></head><body><h1>Hello</h1></body></html>";
125         PrintWriter out=response.getWriter();
126         out.print(html);
127         out.flush();
128         out.close();
129         
130     }
131 
132     @Override
133     public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
134         // TODO Auto-generated method stub
135         this.doGet(request, response);
136     }
137     
138 
139 }
140 package com.wj2;
141 
142 import java.io.IOException;
143 import java.io.PrintWriter;
144 
145 import com.wj.HttpServlet;
146 import com.wj.HttpServletRequest;
147 import com.wj.HttpServletResponse;
148 
149 public class MyServlet2 extends HttpServlet{
150 
151     @Override
152     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
153         // TODO Auto-generated method stub
154         String html = "<html><head><title>hello2</title></head><body><h1>Hello2</h1></body></html>";
155         PrintWriter out=response.getWriter();
156         out.println(html);
157         out.flush();
158         out.close();
159         
160     }
161 
162     @Override
163     public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
164         // TODO Auto-generated method stub
165         this.doGet(request, response);
166     }
167     
168 
169 }
View Code

web.properties内容:(URL,映射到对应的Servlet类)

/testServlet/servlet/SvTest com.wj2.MyServlet
/testServlet/SvTest2 com.wj2.MyServlet2

Java工程目录树为:

模拟Servlet容器管理Servlet

浏览器可以和访问Tomcat服务器一样,我们直接访问:http://localhost:11111/testServlet/Servlet/SvTest

 (ps:如果显示localhost未发送任何数据的结果,可以换一个浏览器进行操作)