设为首页 收藏本站
查看: 498|回复: 0

[经验分享] 请求在Tomcat中传到了CoyoteAdapter的#service()方法中后,就要准备进入Pi

[复制链接]

尚未签到

发表于 2017-2-11 07:08:35 | 显示全部楼层 |阅读模式
首先,来看一下Servlet的载入过程。

具体是在org.apache.catalina.core.StandardWrapper#allocate()这个方法中,源代码如下:








public Servlet allocate() throws ServletException {
if (unloading)
throw new ServletException(sm.getString("standardWrapper.unloading", getName()));
// 是否创建了新的实例
boolean newInstance = false;
// 对非singleThreadModel的Servlet的处理
// 如果不是SingleThreadedModel,每次都返回相同的实例
if (!singleThreadModel) {
// 初始化Servlet实例
if (instance == null) {
synchronized (this) {
if (instance == null) {
try {
if (log.isDebugEnabled())
log.debug("Allocating non-STM instance");
// 载入新的实例
instance = loadServlet();
if (!singleThreadModel) {
newInstance = true;
// 计数器自增
countAllocated++;
}
} catch (ServletException e) {
throw e;
} catch (Throwable e) {
throw new ServletException(sm.getString("standardWrapper.allocate"), e);
}
}
}
}
if (!singleThreadModel) {
if (log.isTraceEnabled())
log.trace("  Returning non-STM instance");
if (!newInstance) {// 没有创建新实例的时候的计数器自增
countAllocated++;
}
// 直接返回Servlet实例
return (instance);
}
}
// 下边是对singleThreadModel的Servlet的处理
synchronized (instancePool) {
while (countAllocated >= nInstances) {
// Allocate a new instance if possible, or else wait
if (nInstances < maxInstances) {
try {
// 将Servlet实例入栈
instancePool.push(loadServlet());
// 实例数自增
nInstances++;
} catch (ServletException e) {
throw e;
} catch (Throwable e) {
throw new ServletException(sm.getString("standardWrapper.allocate"), e);
}
} else {
try {
instancePool.wait();
} catch (InterruptedException e) {
;
}
}
}
if (log.isTraceEnabled())
log.trace("  Returning allocated STM instance");
countAllocated++;
return (Servlet) instancePool.pop();
}
}


这段代码分两种情况来载入Servlet实例,一种是singleThreadModel的,每个请求都要创建一个实例,这些实例是放到instancePool中的,另外一种是非singleThreadModel的,它对每一个请求线程都使用同一个实例。对于非singleThreadModel的Servlet,当第一次载入之后就不需要再次构建了。

下边来大概的看一下具体的载入Servelt的过程,就是#loadServlet()这个方法。方法的细节比较多,大致了解下,主要就是ClassLoader的构建,Servlet实例的创建和Servlet初始化方法的调用3个大的部分。源代码如下:








public synchronized Servlet loadServlet() throws ServletException {
// 非singleThreadModel模式并且instance已经实例化过的情况直接返回
if (!singleThreadModel && (instance != null))
return instance;
PrintStream out = System.out;
if (swallowOutput) {
SystemLogHandler.startCapture();
}
Servlet servlet;
try {
long t1 = System.currentTimeMillis();
// Servlet的完整类名
String actualClass = servletClass;
if ((actualClass == null) && (jspFile != null)) {// 如果是JSP文件
Wrapper jspWrapper = (Wrapper) ((Context) getParent()).findChild(Constants.JSP_SERVLET_NAME);
if (jspWrapper != null) {
actualClass = jspWrapper.getServletClass();
// Merge init parameters
String paramNames[] = jspWrapper.findInitParameters();
for (int i = 0; i < paramNames.length; i++) {
if (parameters.get(paramNames) == null) {
parameters.put(paramNames, jspWrapper.findInitParameter(paramNames));
}
}
}
}
// Complain if no servlet class has been specified
if (actualClass == null) {
unavailable(null);
throw new ServletException(sm.getString("standardWrapper.notClass", getName()));
}
// 构建ClassLoader
Loader loader = getLoader();
if (loader == null) {
unavailable(null);
throw new ServletException(sm.getString("standardWrapper.missingLoader", getName()));
}
ClassLoader classLoader = loader.getClassLoader();
if (isContainerProvidedServlet(actualClass) && !((Context) getParent()).getPrivileged()) {
classLoader = this.getClass().getClassLoader();
}
Class classClass = null;
try {
if (SecurityUtil.isPackageProtectionEnabled()) {
final ClassLoader fclassLoader = classLoader;
final String factualClass = actualClass;
try {
classClass = (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
if (fclassLoader != null) {
return fclassLoader.loadClass(factualClass);
} else {
return Class.forName(factualClass);
}
}
});
} catch (PrivilegedActionException pax) {
Exception ex = pax.getException();
if (ex instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ex;
} else {
getServletContext().log("Error loading " + fclassLoader + " " + factualClass, ex);
}
}
} else {
if (classLoader != null) {
classClass = classLoader.loadClass(actualClass);
} else {
classClass = Class.forName(actualClass);
}
}
} catch (ClassNotFoundException e) {
unavailable(null);
getServletContext().log("Error loading " + classLoader + " " + actualClass, e);
throw new ServletException(sm.getString("standardWrapper.missingClass", actualClass), e);
}
if (classClass == null) {
unavailable(null);
throw new ServletException(sm.getString("standardWrapper.missingClass", actualClass));
}
// 生成Servlet实例
try {
servlet = (Servlet) classClass.newInstance();
if (!((Context) getParent()).getIgnoreAnnotations()) {
if (getParent() instanceof StandardContext) {
((StandardContext) getParent()).getAnnotationProcessor().processAnnotations(servlet);
((StandardContext) getParent()).getAnnotationProcessor().postConstruct(servlet);
}
}
} catch (ClassCastException e) {
unavailable(null);
throw new ServletException(sm.getString("standardWrapper.notServlet", actualClass), e);
} catch (Throwable e) {
unavailable(null);
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardWrapper.instantiate", actualClass), e);
}
throw new ServletException(sm.getString("standardWrapper.instantiate", actualClass), e);
}
if (!isServletAllowed(servlet)) {
throw new SecurityException(sm.getString("standardWrapper.privilegedServlet", actualClass));
}
if ((servlet instanceof ContainerServlet)
&& (isContainerProvidedServlet(actualClass) || ((Context) getParent()).getPrivileged())) {
((ContainerServlet) servlet).setWrapper(this);
}
classLoadTime = (int) (System.currentTimeMillis() - t1);
// 调用Servelt的初始化方法
try {
instanceSupport.fireInstanceEvent(InstanceEvent.BEFORE_INIT_EVENT, servlet);
if (Globals.IS_SECURITY_ENABLED) {
Object[] args = new Object[] { ((ServletConfig) facade) };
SecurityUtil.doAsPrivilege("init", servlet, classType, args);
args = null;
} else {
servlet.init(facade);
}
// Invoke jspInit on JSP pages
if ((loadOnStartup >= 0) && (jspFile != null)) {
// 调用jspInit()
DummyRequest req = new DummyRequest();
req.setServletPath(jspFile);
req.setQueryString("jsp_precompile=true");
DummyResponse res = new DummyResponse();
if (Globals.IS_SECURITY_ENABLED) {
Object[] args = new Object[] { req, res };
SecurityUtil.doAsPrivilege("service", servlet, classTypeUsedInService, args);
args = null;
} else {
servlet.service(req, res);
}
}
instanceSupport.fireInstanceEvent(InstanceEvent.AFTER_INIT_EVENT, servlet);
} catch (UnavailableException f) {
instanceSupport.fireInstanceEvent(InstanceEvent.AFTER_INIT_EVENT, servlet, f);
unavailable(f);
throw f;
} catch (ServletException f) {
instanceSupport.fireInstanceEvent(InstanceEvent.AFTER_INIT_EVENT, servlet, f);
throw f;
} catch (Throwable f) {
getServletContext().log("StandardWrapper.Throwable", f);
instanceSupport.fireInstanceEvent(InstanceEvent.AFTER_INIT_EVENT, servlet, f);
throw new ServletException(sm.getString("standardWrapper.initException", getName()), f);
}
singleThreadModel = servlet instanceof SingleThreadModel;
if (singleThreadModel) {
if (instancePool == null)
instancePool = new Stack();
}
fireContainerEvent("load", this);
loadTime = System.currentTimeMillis() - t1;
} finally {
if (swallowOutput) {
String log = SystemLogHandler.stopCapture();
if (log != null && log.length() > 0) {
if (getServletContext() != null) {
getServletContext().log(log);
} else {
out.println(log);
}
}
}
}
return servlet;
}

另外,在载入JSP文件时,Servlet是org.apache.jasper.servlet.JspServlet的实例,载入静态资源时,Servlet用的是org.apache.catalina.servlets.DefaultServlet实例。

而JSP页面在后边还有编译等操作,后边还要继续详述。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-340391-1-1.html 上篇帖子: 如何在Tomcat服务器的工程中使用---listener 下篇帖子: 将免安装版本的tomcat注册为windows系统服务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表