10/18/2011 2:38:04 AM
Viewed: 2041
teanjava
9/20/2011 2:47:09 AM
Difference between static include and dynamic include in JSP
I thought of starting with a usual JSP interview question. So I asked him what is the difference between static include and dynamic include in JSP. His answer was partially correct. So I though I will write a post on what are the differences between static include and dynamic include in JSP. So the difference between static include and dynamic include are:

  • The syntax for static include is <%@ include file=”filename.jsp” %> and
the syntax for dynamic include is <jsp:include page=”filename.jsp” />
  • Static include is an inline inclusion. i.e., the contents of the file will be included at translation phase. It’s something like a copy and paste :-) . In case of dynamic include the response of the file will be included to the original response at runtime. The file will be processed separately and only the response will become the part of the original files’ response.
  • Static include cannot have a dynamic filename. This is because the servlet container needs the files for inclusion, at translation phase itself. But dynamic include can have a dynamic filename. Here the file is getting included at runtime.
  • Static include cannot accept a parameter (What this parameter will do even if are passing it?). But dynamic include can accept a parameter. (Here we have some one to do something on the parameter).
  • Static includes are faster than dynamic includes.
  • What will happen if unfortunately the resource lost at static include?
    In static include if the resource is not available, the container would throw an internal server error ie, Error 500 (At least in WebSphere). Ie, for static include the resource has to be present at translation phase. Once the page is compiled, even if the resource is not available, static include would work as expected. This is because the resource is already a part of the main file and it will not look for the file at run time.

    Dynamic include would throw a Page not found (HTTP Error 404), if the resource is not available at run time. The resource has to be present in the server every time we do a lookup for the file. This is because dynamic include uses a runtime inclusion of the file.

  • what is the original jsp or servlet file length in both cases?
    In case of static include the original file size will be original file size included file size. Static include uses an inline inclusion. So the included file will become the part of original file at translation phase.

    In case of dynamic include, there will not be any change in the size of original file.

  • Which one is efficient for static content and dynamic content?
    Static includes are more efficient for static content while dynamic include is more efficient for dynamic content.

I believe these are the differences between static include and dynamic include. I think I have covered all. If I missed anyone, please let me know.


Having Tea And Feeling Java

Gửi trả lời