- 博客分类:
- jetty
1.概述
jetty会将jsp文件编译成servlet然后提供服务,主要原理是在webdefault.xml中,有一个关于jsp的配置
- <</SPAN>servlet
id="jsp"> -
<</SPAN>servlet-name>jsp</</SPAN>servlet-name> -
<</SPAN>servlet-class>org.apache.jasper.servlet.JspServlet</</SPAN>servlet-class> - </</SPAN>servlet>
jsp org.apache.jasper.servlet.JspServlet
因此所有的jsp的请求都会由这个servlet来处理
jetty对于jsp的支持以及依赖的jar包

jetty用的的jsp-2.1-glassfish的核心类图如下:
2.编译
2.1 编译过程
编译的逻辑是(jsp2.1.6.jar),在每次请求JspServlet的service方法中,主要调用
serviceJspFile(request, response, jspUri, null, precompile);
然后调用
wrapper = (JspServletWrapper) rctxt.getWrapper(jspUri);
根据jsp的请求路径取得对应的JspServletWrapper(wrapper中就会有jspfile的路径)。然后将请求委托到JspServletWrapper,这个类的逻辑如下:
(1) Compile,调用Compiler的compile方法,然后调用ecj.jar或者jdk的javacompiler来编译并生成对应的class。(编译的过程主要是generateJava() ;generateclass();两个步骤)
(2) (Re)load servlet class file,并赋值给theServlet成员变量
(3) Service request ,调用servlet的服务
2.2 配置
development参数,默认是true,如果不设置,每次访问都会重新编译jsp文件,引起性能问题。
- <</SPAN>init-param>
-
<</SPAN>param-name>development</</SPAN>param-name> -
<</SPAN>param-value>false</</SPAN>param-value> -
</</SPAN>init-param>
development
false
如果设置了上面的参数,但是需要在修改jsp文件后能够动态生效,需要在webdefault.xml中添加如下配置:
- <</SPAN>init-param>
-
<</SPAN>param-name>checkInterval</</SPAN>param-name> (默认是0,也就是不扫描) -
<</SPAN>param-value>5</</SPAN>param-value> -
</</SPAN>init-param>
checkInterval (默认是0,也就是不扫描)
5
3. compiler的选择
都是使用javaComplier,从初始化逻辑可以看到,先尝试使用JDK的complier,不行就使用ecj的compiler。
- private
void initJavaCompiler() { -
Class c = getClassFor("javax.tools.ToolProvider"); -
if (c != null) { -
// JDK1.6 -
c = getClassFor("org.apache.jasper.compiler.Jsr199JavaCompiler"); -
if (c != null) { -
try { -
javaCompiler = (JavaCompiler) c.newInstance(); -
} catch (Exception ex) { -
} -
} -
} -
if (javaCompiler == null) { -
c = getClassFor("org.eclipse.jdt.internal.compiler.Compiler"); -
if (c != null) { -
c = getClassFor("org.apache.jasper.compiler.JDTJavaCompiler"); -
if (c != null) { -
try { -
javaCompiler = (JavaCompiler) c.newInstance(); -
} catch (Exception ex) { -
} -
} -
} -
} -
if (javaCompiler == null) { -
javaCompiler = new AntJavaCompiler(); -
} -
-
javaCompiler.init(ctxt, errDispatcher, jspcMode); - }
private void initJavaCompiler() {
Class c = getClassFor("javax.tools.ToolProvider");
if (c != null) {
// JDK1.6
c = getClassFor("org.apache.jasper.compiler.Jsr199JavaCompiler");
if (c != null) {
try {
javaCompiler = (JavaCompiler) c.newInstance();
} catch (Exception ex) {
}
}
}
if (javaCompiler == null) {
c = getClassFor("org.eclipse.jdt.internal.compiler.Compiler");
if (c != null) {
c = getClassFor("org.apache.jasper.compiler.JDTJavaCompiler");
if (c != null) {
try {
javaCompiler = (JavaCompiler) c.newInstance();
} catch (Exception ex) {
}
}
}
}
if (javaCompiler == null) {
javaCompiler = new AntJavaCompiler();
}
javaCompiler.init(ctxt, errDispatcher, jspcMode);
}
本文介绍了Jetty服务器如何处理JSP文件,通过将其编译为Servlet来提供服务。主要内容包括Jetty中JSP的配置方式、编译过程详解、编译器选择策略及配置建议。

1万+

被折叠的 条评论
为什么被折叠?



