Do not go gentle into that good night
这篇博源于之前遇到过的一个时常发生的服务器错误,经过简单的测试之后发现了原因;
介于最近工作比较清闲,便花了些时间研究了下这个问题,文中有不足的地方还希望能有大神多多指教;
开发环境:
Eclipse 4.3
jdk1.7.0_72
apache-tomcat-7.0.53
spring-mvc-4.0
spring-beans-4.0
先说说遇到的问题,我发现在项目编译过程中偶尔会遇到500错误,通常重启一下tomcat就能解决;后来几次报错中,我发现似乎都是在报同一个错误:java.lang.ClassNotFoundException: org.apache.jsp.views.base.***_jsp
通配符部分是我当时访问的jsp文件名;
于是我开始注意触发这个错误的条件,发现这个问题总是在我编辑jsp页面保存之后访问页面出现;经过测试后证实了引发这个问题的具体操作顺序——
A:
a. CAS
b. tomcat start
c. jsp– >editor– >save
d. jsp
B:
a. tomcat start
b. jsp– >editor– >save
c. CAS
d. jsp
C:
a. jsp– >editor– >save
b. tomcat start
c. CAS
d. jsp
这三种情况中,只有C情况能正常访问,另外两种都会在访问的时候报出500错误;关于cas部可以忽略,因为工作项目配置了单点登陆,所以访问之前都会先进入cas进行登陆;
到这里就很容易能够猜测到问题原因,我们都知道jsp的本质是servlet(可以这么说),当访问一个jsp的时候容器会将其编译成一个运行在服务器上的servlet来处理页面访问请求;我在启动tomcat之后再编辑并保存jsp,当发起页面访问请求的时候,因为某组件访问到了未被编译的页面,所以会导致出现ClassNotFound;
上面的描述虽然不太准确,但基本能解释引发上面500错误的原因,剩下的问题就是来看一下tomcat具体是怎么执行客户端对于一个jsp的访问请求的;我试着列举了一下需要弄清楚的问题清单——
- 用户在地址栏中输入访问url,这个过程是如何与服务器交互的;
- 用户是如何查看到显示到浏览器窗口中的页面的;
- 如果用户访问的是服务器上的一个文件,那么它存放在哪里;
- 如果上一条成立,那么这个文件是如何生成的;
对于第一条,伯乐在线有两篇文章专门探讨了这个问题,它详细分析了当我们在地址栏 输入一个url之后会发生什么事情,这里就不做深讨;
对于第二个问题,我们都知道用户在浏览器窗口看到的页面实质上是被浏览器解析出来的HTML代码,具体过程在上面两篇文章中也有提到;那么回忆一下最初的问题,当访问某个页面的时候会出现500错误,而原因是因为我修改并保存了对应的JSP文件;
由此可以推断:我在项目中编写的jsp被某种机制编译成了显示在页面上的HTML代码,而用户访问的页面代码实质上也来自于这些代码所在的文件;那么接下来就是证实第三和第四个问题了;
这个问题需要了解tomcat将编译后的项目放在了什么地方;这部分知识深究下去也很有意思,但目前所学还没涉及到这方面,简单的知道一件事就行——tomcat会把项目编译并部署到一个路径下,这个路径似乎可以通过eclipse进行配置,页面如下:
经过后期的测试,会发现当项目启动后,用户发出一个访问请求,所生成的文件会集中到这个目录中,下面的截图是我按照时间进行的
可以看到,目前在tomcat未启动的情况下,这个目录是空的;下面我会继续测试当启动了部署我项目的tomcat之后以及随后发生的事情,在这之前先看一下一个为了测试简单编写的jsp页面,以及项目中的绝对路径;
/cicada/src/main/webapp/views/demo/demo.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@taglib uri="/ms-tags" prefix="ms"%>
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<c:import url="/public/include/msDocType.html" />
<html>
<head>
<c:import url="/public/include/msMetaList.jsp" />
</head>
<body>
<c:import url="/public/include/msTitle.jsp" />
</body>
</html>
接着我启动tomcat,看看上面的目录会发生什么变化;
可以看到tomcat在这个名为localhost的目录下生成了一系列的文件;注意一下这里有个名为“_”的目录,这让我联想到了那个报错;
java.lang.ClassNotFoundException: org.apache.jsp.views.base.***_jsp
和编译器打交道久了会有一个发现“报错提示中的任何字符都是有意义的”,其实这里的“_jsp”也是有意义的,我们可以看看这个下划线目录里都有什么;
啥都没有对吧,那是自然,因为我还没访问页面;也就是说客户端还没有发起访问请求;那么接着对上面那个jsp文件发起一次访问;看看这里会出现什么;
看看子目录里都有啥;
看来已经接近答案了;
我们来看看这个java文件里面写了啥,一大片有木有,其实可以不用仔细看的,因为我也没有仔细看=v=;
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.53
* Generated at: 2015-11-05 08:13:12 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp.views.demo;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class demo_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
_jspx_dependants.put("/WEB-INF/ms-tags.tld", Long.valueOf(1446695246052L));
}
private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
_005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.release();
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
if (_jspx_meth_c_005fimport_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
if (_jspx_meth_c_005fimport_005f1(_jspx_page_context))
return;
out.write("\r\n");
out.write("\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t");
if (_jspx_meth_c_005fimport_005f2(_jspx_page_context))
return;
out.write("\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
private boolean _jspx_meth_c_005fimport_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// c:import
org.apache.taglibs.standard.tag.rt.core.ImportTag _jspx_th_c_005fimport_005f0 = (org.apache.taglibs.standard.tag.rt.core.ImportTag) _005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.ImportTag.class);
_jspx_th_c_005fimport_005f0.setPageContext(_jspx_page_context);
_jspx_th_c_005fimport_005f0.setParent(null);
// /views/demo/demo.jsp(5,0) name = url type = null reqTime = true required = true fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fimport_005f0.setUrl("/public/include/msDocType.html");
int[] _jspx_push_body_count_c_005fimport_005f0 = new int[] { 0 };
try {
int _jspx_eval_c_005fimport_005f0 = _jspx_th_c_005fimport_005f0.doStartTag();
if (_jspx_th_c_005fimport_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
return true;
}
} catch (java.lang.Throwable _jspx_exception) {
while (_jspx_push_body_count_c_005fimport_005f0[0]-- > 0)
out = _jspx_page_context.popBody();
_jspx_th_c_005fimport_005f0.doCatch(_jspx_exception);
} finally {
_jspx_th_c_005fimport_005f0.doFinally();
_005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.reuse(_jspx_th_c_005fimport_005f0);
}
return false;
}
private boolean _jspx_meth_c_005fimport_005f1(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// c:import
org.apache.taglibs.standard.tag.rt.core.ImportTag _jspx_th_c_005fimport_005f1 = (org.apache.taglibs.standard.tag.rt.core.ImportTag) _005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.ImportTag.class);
_jspx_th_c_005fimport_005f1.setPageContext(_jspx_page_context);
_jspx_th_c_005fimport_005f1.setParent(null);
// /views/demo/demo.jsp(8,0) name = url type = null reqTime = true required = true fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fimport_005f1.setUrl("/public/include/msMetaList.jsp");
int[] _jspx_push_body_count_c_005fimport_005f1 = new int[] { 0 };
try {
int _jspx_eval_c_005fimport_005f1 = _jspx_th_c_005fimport_005f1.doStartTag();
if (_jspx_th_c_005fimport_005f1.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
return true;
}
} catch (java.lang.Throwable _jspx_exception) {
while (_jspx_push_body_count_c_005fimport_005f1[0]-- > 0)
out = _jspx_page_context.popBody();
_jspx_th_c_005fimport_005f1.doCatch(_jspx_exception);
} finally {
_jspx_th_c_005fimport_005f1.doFinally();
_005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.reuse(_jspx_th_c_005fimport_005f1);
}
return false;
}
private boolean _jspx_meth_c_005fimport_005f2(javax.servlet.jsp.PageContext _jspx_page_context)
throws java.lang.Throwable {
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// c:import
org.apache.taglibs.standard.tag.rt.core.ImportTag _jspx_th_c_005fimport_005f2 = (org.apache.taglibs.standard.tag.rt.core.ImportTag) _005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.ImportTag.class);
_jspx_th_c_005fimport_005f2.setPageContext(_jspx_page_context);
_jspx_th_c_005fimport_005f2.setParent(null);
// /views/demo/demo.jsp(12,1) name = url type = null reqTime = true required = true fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fimport_005f2.setUrl("/public/include/msTitle.jsp");
int[] _jspx_push_body_count_c_005fimport_005f2 = new int[] { 0 };
try {
int _jspx_eval_c_005fimport_005f2 = _jspx_th_c_005fimport_005f2.doStartTag();
if (_jspx_th_c_005fimport_005f2.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
return true;
}
} catch (java.lang.Throwable _jspx_exception) {
while (_jspx_push_body_count_c_005fimport_005f2[0]-- > 0)
out = _jspx_page_context.popBody();
_jspx_th_c_005fimport_005f2.doCatch(_jspx_exception);
} finally {
_jspx_th_c_005fimport_005f2.doFinally();
_005fjspx_005ftagPool_005fc_005fimport_0026_005furl_005fnobody.reuse(_jspx_th_c_005fimport_005f2);
}
return false;
}
}
到这里基本已经能够明白发生的事情;
总结:
当我们编辑好一个jsp页面之后,如果启动tomcat,当它部署好工程之后会加载工程的目录到某个路径中,当tomcat启动完成(这点很重要)之后如果对jsp页面发起一个访问请求,那么这个页面会被编译并生成对应的class文件和java文件,而它具体做的事情就是向页面以流的形式输出对应的HTML标签,再交由浏览器解析最终生成我们看到的页面;
而之所以发生500错误,是因为我在tomcat未完成一次编译之后对文件进行了修改,当用户发起访问请求时,访问到了未被编译的class文件,所以才会出现最初的异常,下面的几篇文章更详细的解释了关于生成的java代码中的细节,可以参考;
【Reference Material】
【End】