Jsp: inclusion, performance, modulation, alternatives and best practices, part 96

This is the following answer to the question "jsp include overhead" below:

JSP performance with jsp: include

In our application, developers have “modular” jsp fragments, using “jsp: includes” for “generic” jsp code that repeats throughout the application.

Pros

Pros are as follows:

  • it DRY - we define the jsp fragment once. This is a great help when you need to change some html and do not need to search / replace / search / destroy.

  • pretty easy to follow: you pass parameters clearly. When you edit the “included” page, you “know what you are getting,” that is, against some of the “global variables” declared on the inclusion / invocation page.

against

  • overhead query performance data

Questions

So, as a continuation:

  • How much performance overhead does jsp: include have? This is not obvious from tomcat code (although you can see that it is much more than an inline call). Also, in the profiling of an application that I never requested, the dispasscher.include () or invoke () methods appear as hotspots.
  • - , ? (.. X Y). " " (, GC), ?
  • ? (AFAIK @include jsp: - ?)
  • ( ), jsp , .. " ", "jsp: include" '@include'.

. jsp.

tomcat applicationDispatcher.invoke() (tomcat 5.5. , ). .

    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 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;

}
+3
1

, - <jsp:include>, . Tomcat HttpServletRequest , , , , . , , X , .

<jsp:include> JSP 2.0. , , <jsp:include>, - ( ) <jsp:include>. , , .

( ) , : <%@ include file="x.jsp" %>. . , , , x.jsp , "" .

+3

Source: https://habr.com/ru/post/1757041/


All Articles