JAVA手写tomcat,带你了解tomcat的原理,附代码( 六 )


if (tag.equals("servlet")) {
isMapping = false;
} else if (tag.equals("servlet-mapping")) {
isMapping = true;
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String contents = new String(ch, start, length).trim();
if (null != contents) {// 处理空的问题
if (isMapping) {// 操作servlet-mapping
if (tag.equals("servlet-name")) {
currentServletMapping = contents;
} else if (tag.equals("url-pattern")) {
String urlPattern = contents;
servletMapping.put(urlPattern, currentServlet);
}
} else {// 操作servlet
if (tag.equals("servlet-name")) {
currentServlet = contents;
currentServletMapping = contents;
} else if (tag.equals("servlet-class")) {
String servletClass = contents;
servlets.put(currentServlet, servletClass);
}
}
}
}
}
说明本次代码没有实现request和response是的交互 , 返回都是写死的outputStream.write(“Hello World!”.getBytes()) 。
如果需要实现 , 需要写自己的request和response 。

JAVA手写tomcat,带你了解tomcat的原理,附代码

文章插图




推荐阅读