编程题
开发Minicat V4.0,在已有Minicat基础上进一步扩展,模拟出webapps部署效果 磁盘上放置一个webapps目录,webapps中可以有多个项目,例如demo1、demo2、demo3… 每个项目中含有servlet,可以根据请求url定位对应servlet进一步处理。
解答:
1)增加 server.xml配置文件,其中app元素指定appBase路径
<?xml version="1.0" encoding="UTF-8" ?>
<Server>
<Service>
<Connect port="8081"/>
<Engine>
<Host name="localhost" appBase="C:\lagou-java\tomcat\TomcatNginx资料\代码\webapps1.0"></Host>
</Engine>
</Service>
</Server>
2)把request、response以及Servlet和它的抽象实现类HttpServlet抽取出来到servlet-api子模块,供webapp扩展使用
![输入图片说明](https://img-blog.csdnimg.cn/img_convert/0bf8332409c3725f17714ea5aaf88f62.png
3)改造之前的Bootstrap的loadServlet方法,使其去加载server.xml配置文件,获取到appBase参数值
然后到appBase指定的路径,读取各个web app,解析其中WEB-INF下的web.xml,并使用自定类加载器加载servlet的class及对应webapp下的关联class
private void loadServletByMany() {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
SAXReader saxReader = new SAXReader();
try {
//解析server.xml
Document document = saxReader.read(resourceAsStream);
Element rootElement = document.getRootElement();
List<Element> portNodes = rootElement.selectNodes("//Connect");
Element element = (Element) rootElement.selectSingleNode("//Connect");
port = Integer.parseInt(element.attributeValue("port"));
List<Element> hostsEle = rootElement.selectNodes("//Host");//获取host
for (int i = 0; i <hostsEle.size() ; i++) {
String hostName = hostsEle.get(i).attributeValue("name");
//项目集
Map<String,Map<String,HttpServlet>> contexts =new HashMap<>();
webappsPath = hostsEle.get(i).attributeValue("appBase");//host对应项目路径路径
webappsPath = webappsPath.replaceAll("\\\\","/");
File file = new File(webappsPath);
if(file.exists() && !file.isFile()) {
File [] files = file.listFiles();
for (int j = 0; j < files.length; j++) {
//项目
Map<String,HttpServlet> context = new HashMap<>();
String webPath = files[j].getPath();//项目路径
//单个项目文件名
String fileName = webPath.replace(file.getPath()+"\\","");
//读取web.xml文件
File webXml = new File(webPath+"\\web.xml");
InputStream resourceAsStreamWeb = new FileInputStream(webXml);
SAXReader saxReaderWeb = new SAXReader();
Map<String,HttpServlet> wrapper = new HashMap<String,HttpServlet>();
//解析各个项目web.xml
try {
Document documentWeb = saxReaderWeb.read(resourceAsStreamWeb);
Element rootElementWeb = documentWeb.getRootElement();
List<Element> selectNodes = rootElementWeb.selectNodes("//servlet");
for (int k = 0; k < selectNodes.size(); k++) {
Element elementWeb = selectNodes.get(k);
// <servlet-name>lagou</servlet-name>
Element servletnameElement = (Element) elementWeb.selectSingleNode("servlet-name");
String servletName = servletnameElement.getStringValue();
// <servlet-class>server.LagouServlet</servlet-class>
Element servletclassElement = (Element) elementWeb.selectSingleNode("servlet-class");
String servletClass = servletclassElement.getStringValue();//class绝对路径
// 根据servlet-name的值找到url-pattern
Element servletMapping = (Element) rootElementWeb.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
// /lagou
String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
String pakageDir = webPath.replaceAll("\\\\","/");
MyClassLoader loader = new MyClassLoader(pakageDir+"/");
Class<?> clazz = loader.findClass(servletClass);
wrapper.put(urlPattern, (HttpServlet) clazz.getClassLoader().loadClass(servletClass).newInstance());
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//context.put(fileName,);
contexts.put(fileName,wrapper);
}
}
if("localhost".equalsIgnoreCase(hostName)){
hosts.put("127.0.0.1",contexts);//增加127.0.0.1 对应 localhost
}
hosts.put(hostName,contexts);
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
4)通过依照servlet-api的servlet接口,开发自己的Servlet类,并在WEB-INF目录下配置web.xml配置文件
把构建好的项目目录拷贝到server.xml指定的webapps路径下,启动minicat项目,通过浏览器访问
视频地址 : https://v.qq.com/x/page/r32472koem7.html
代码地址 :https://gitee.com/ou_song/tomcat-mini
作业⼆(简答题)
请详细描述Tomcat体系结构(图⽂并茂)
组件 | 作用描述 |
---|---|
Server | 是Tomcat构成的顶级构成元素,所有一切均包含在Server中,Server的实现类StandardServer可以包含一个到多个Services |
Service | Service的实现类为StandardService调用了容器(Container)接口,其实是调用了Servlet Engine(引擎),而且StandardService类中也指明了该Service归属的Server; |
Connector | 将Service和Container连接起来,首先它需要注册到一个Service,它的作用就是把来自客户端的请求转发到Container(容器) |
Container | 引擎(Engine)、主机(Host)、上下文(Context)和Wraper均继承自Container接口,所以它们都是容器。但是,它们是有父子关系的,在主机(Host)、上下文(Context)和引擎(Engine)这三类容器中,引擎是顶级容器,直接包含是主机容器,而主机容器又包含上下文容器,所以引擎、主机和上下文从大小上来说又构成父子关系,虽然它们都继承自Container接口 |
EndPoint | EndPoint 是 Coyote 通信端点,即通信监听的接口,是具体Socket接收和发送处理器,是对传输层的抽象,因此EndPoint用来实现TCP/IP协议的 |
Processor | Processor 是Coyote 协议处理接口,如果说EndPoint是用来实现TCP/IP协议的,那么Processor用来实现HTTP协议,Processor接收来自EndPoint的Socket,读取字节流解析成Tomcat Request和Response对象,并通过Adapter将其提交到容器处理,Processor是对应用层协议的抽象 |
ProtocolHandler | Coyote 协议接口,通过Endpoint 和 Processor ,实现针对具体协议的处理能力。Tomcat 按照协议和I/O 提供了6个实现类: AjpNioProtocol ,AjpAprProtocol, AjpNio2Protocol , Http11NioProtocol ,Http11Nio2Protocol ,Http11AprProtocol |
Adapter | 由于协议不同,客户端发过来的请求信息也不尽相同,Tomcat定义了自己的Request类来封装这些请求信息。ProtocolHandler接口负责解析请求并生成Tomcat Request类。但是这个Request对象不是标准的ServletRequest,不能用Tomcat Request作为参数来调?容器。Tomcat设计者的解决方案是引入CoyoteAdapter,这是适配器模式的经典运用,连接器调用CoyoteAdapter的Sevice方法,传入的是Tomcat Request对象,CoyoteAdapter负责将Tomcat Request转成ServletRequest,再调用容器 |
Host | 虚拟主机 |
Context | 一个Context就是一个web app |
Wrapper | Servlet包装类 |