Java jsp:include,性能,模块化,备选方案和最佳实践,第96部分
这是以下“jsp包含的开销”问题的后续问题: 在我们的应用程序中,开发人员通过大量使用“jsp:includes”对整个应用程序中重复的“公共”jsp代码进行“模块化”jsp片段 专业人士 专业人士的意见如下:Java jsp:include,性能,模块化,备选方案和最佳实践,第96部分,java,performance,jsp,servlets,include,Java,Performance,Jsp,Servlets,Include,这是以下“jsp包含的开销”问题的后续问题: 在我们的应用程序中,开发人员通过大量使用“jsp:includes”对整个应用程序中重复的“公共”jsp代码进行“模块化”jsp片段 专业人士 专业人士的意见如下: 它是干的——我们只定义一次jsp片段。当您需要更改一些html而不需要查找/替换/搜索/销毁时,这是一个很大的帮助 这很容易理解:您可以清楚地传递参数。当您编辑“包含”页面时,您“知道您得到了什么”,即与“包含/调用”页面中声明的一些“全局变量”相比 缺点 附加请求的性能开销
- 它是干的——我们只定义一次jsp片段。当您需要更改一些html而不需要查找/替换/搜索/销毁时,这是一个很大的帮助
- 这很容易理解:您可以清楚地传递参数。当您编辑“包含”页面时,您“知道您得到了什么”,即与“包含/调用”页面中声明的一些“全局变量”相比
- 附加请求的性能开销
- “jsp:include”会产生多少性能开销?从tomcat代码中看不出这一点(尽管您看到它比内联调用做的要多得多)。此外,在分析应用程序时,我从未请求过Dispatcher.include()或invoke()方法显示为热点
- 有人能指出头顶的大部分究竟在哪里吗?(即类Y中的方法X)或者是否只是每个请求发生的所有“小事情”(例如设置属性或对象创建和后续GC)
- 有哪些替代方案?(AFAIK@include和jsp:include.还有别的吗?)
- (愚蠢的额外问题)为什么servlet引擎不能在编译时“包含”jsp,比如像“带参数的内联宏”,这样我们开发人员就可以清楚地看到“jsp:include”和“@include”的性能李>
private void invoke(ServletRequest请求、ServletResponse响应、,
状态)抛出IOException、ServletException{
//检查上下文类加载器是否为当前上下文
//如果不是,我们将保存它,并设置上下文
//类加载器到上下文类加载器
ClassLoader oldCCL=Thread.currentThread().getContextClassLoader();
ClassLoader contextClassLoader=context.getLoader().getClassLoader();
if(oldCCL!=contextClassLoader){
Thread.currentThread().setContextClassLoader(contextClassLoader);
}否则{
oldCCL=null;
}
//初始化我们可能需要的局部变量
HttpServletResponse hresponse=(HttpServletResponse)响应;
Servlet=null;
IOException IOException=null;
ServletException ServletException=null;
RuntimeException RuntimeException=null;
布尔值=假;
//检查标记为不可用的servlet
if(wrapper.isUnavailable()){
wrapper.getLogger().warn(
sm.getString(“applicationDispatcher.isUnavailable”,
wrapper.getName());
long available=wrapper.getAvailable();
如果((可用>0L)和(&(可用
如果您有
private void invoke(ServletRequest request, ServletResponse response,
State state) throws IOException, ServletException {
// Checking to see if the context classloader is the current context
// classloader. If it's not, we're saving it, and setting the context
// classloader to the Context classloader
ClassLoader oldCCL = Thread.currentThread().getContextClassLoader();
ClassLoader contextClassLoader = context.getLoader().getClassLoader();
if (oldCCL != contextClassLoader) {
Thread.currentThread().setContextClassLoader(contextClassLoader);
} else {
oldCCL = null;
}
// Initialize local variables we may need
HttpServletResponse hresponse = (HttpServletResponse) response;
Servlet servlet = null;
IOException ioException = null;
ServletException servletException = null;
RuntimeException runtimeException = null;
boolean unavailable = false;
// Check for the servlet being marked unavailable
if (wrapper.isUnavailable()) {
wrapper.getLogger().warn(
sm.getString("applicationDispatcher.isUnavailable",
wrapper.getName()));
long available = wrapper.getAvailable();
if ((available > 0L) && (available < Long.MAX_VALUE))
hresponse.setDateHeader("Retry-After", available);
hresponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, sm
.getString("applicationDispatcher.isUnavailable", wrapper
.getName()));
unavailable = true;
}
// Allocate a servlet instance to process this request
try {
if (!unavailable) {
servlet = wrapper.allocate();
}
}
...exception handling here....
// Get the FilterChain Here
ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance();
ApplicationFilterChain filterChain = factory.createFilterChain(request,
wrapper,servlet);
// Call the service() method for the allocated servlet instance
try {
String jspFile = wrapper.getJspFile();
if (jspFile != null)
request.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
else
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.BEFORE_DISPATCH_EVENT,
servlet, request, response);
// for includes/forwards
if ((servlet != null) && (filterChain != null)) {
filterChain.doFilter(request, response);
}
// Servlet Service Method is called by the FilterChain
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
}
...exception handling here....
// Release the filter chain (if any) for this request
try {
if (filterChain != null)
filterChain.release();
}
...exception handling here....
// Deallocate the allocated servlet instance
try {
if (servlet != null) {
wrapper.deallocate(servlet);
}
}
...exception handling here....
// Reset the old context class loader
if (oldCCL != null)
Thread.currentThread().setContextClassLoader(oldCCL);
// Unwrap request/response if needed
// See Bugzilla 30949
unwrapRequest(state);
unwrapResponse(state);
// Rethrow an exception if one was thrown by the invoked servlet
if (ioException != null)
throw ioException;
if (servletException != null)
throw servletException;
if (runtimeException != null)
throw runtimeException;
}