• Entries (RSS)
  • Comments (RSS)

Difference between static include and dynamic include in JSP

Posted by | Posted in JSP | Posted on 22-05-2008

Tagged Under : ,

Last day I was taking an interview in my company. The interview was for WebSphere Commerce. So I thought of going directly to WCS questions. But that guy said that he was working on WCS long back and he forgot everything in WebSphere Commerce. Then I thought I will ask some JSP questions as he was mentioned JSP in each and every project in his resume.

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.

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.

Share

Read More

Comments

29 comments posted onDifference between static include and dynamic include in JSP

  1. Good post. I think you have covered almost all the differences.

  2. Nice post, but u missed some more points such as

    –>What will happen if unfortunately the resource lost at static include?

    –>what is the original jsp or servlet file length in both cases?

    –>Which one is efficient for static content and dynamic content?

  3. Thanks Praveen, for pointing out the missing points.

    –>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.

  4. very nice article I found total information regarding this static and dynemic include.
    Thank you very much Albin Joseph

  5. Straight to the point and exact illustration but you forgot to put site an example. I have recently moved from PHP to JSP and I am seeing a lot of downside on JSP’s part. Anyway, just for the fun of it I will continue learning it. Anyway, can you please tell me why this thing doesn’t work?

    MODULE  :
    PAGE    :
    MODFILE :
    

    <%@include file=”" %>

    Error is below:

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: /index.jsp(16,0) File “/” not found
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
    org.apache.jasper.compiler.Parser.processIncludeDirective(Parser.java:345)
    org.apache.jasper.compiler.Parser.parseIncludeDirective(Parser.java:378)
    org.apache.jasper.compiler.Parser.parseDirective(Parser.java:486)
    org.apache.jasper.compiler.Parser.parseElements(Parser.java:1444)
    org.apache.jasper.compiler.Parser.parse(Parser.java:138)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:216)
    org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:154)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:315)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.

  6. I feel the file that you are including does not exist. We will get an internal server error if the resource we are trying to include does not exist. So please have a look at < %@include file=”" %> statement.

    Is that file passed as the value for attribute ‘file’ exist?

  7. Thanks Albin. I got to the root of the error now. I was actually trying to move my existing site framework I created in PHP 4 years ago into JSP. I found out that unlike PHP, variables in JSP become invisible inside “included” files using the directive. i was trying to load a file dynamically depending on the value of the request querystring.

    I was tying out some workaround and moving my variables one step up and replaced the old include with <jsp:include

    Even the < %@taglib from the calling page is invisible from the included files. I know it’s me but I’m not sure where I am doing it wrong.

    Sorry, first day with JSP.

  8. OK, just a follow up.

    I did a bit of review and a lot of revisions on my codes and found out that replacing the variable initializations can help make the variables become visible to all pages of the application:

    Replaced codes like these

    with something like these:

    <c:out value=”" default=”home” />

    I would love to use the first one but I can’t somehow make it available globally throughout the application. I have a feeling that files are being compiled separately before incorporating the output together unlike in PHP where source codes are merged first by the interpreter before compilation — or at least that’s what I think is happening. Is this the case or am I just missing something really important here?

  9. When we user <%@include, both the fiels will be compiled separately?

  10. @ash, when we use @include both the files will be compiled as a single unit.

  11. Thanks, guys! I finally get a grip of JSP. I still prefer how PHP handles variables but, anyway, I love how NetBeans made things a lot easier so I guess I’m finally putting JSP on my fave list.

  12. Guys,
    Any idea why this fails:
    [404:File not found calendar.js]
    while this is successfull

    This happens on WebSphere 5.1.1.19 but on a previous patch 5.1.1.3 it works.
    What could be the reason?

  13. Well the post has garbled my message

    ” ” throws 404 file not found
    ” ” works

  14. Good post, but what’s the reason that static include does not accept any parameters
    for example

  15. include file=”1.jsp?param=param1″

  16. Its because, the file will be included inline at translation phase. Parameters make some sense only if we pass it at run time.

  17. Dude u r a rockstar….
    call me up.

  18. Nice and simple post…helped to solve my issues

    Thanks

  19. Very nice to see this article and discussion..
    Thanks…

  20. Thanks a lot for such a great distinction between two.
    I wanted to know when we should use dynamic include instead of static include.
    As per my understanding after reading your post, we (should) use dynamic include only when we want to include file at runtime. Is there any other scenario where dynamic include should be used?

  21. Hi Albin i have a small query regarding multithreading?

    in multithreading we can create thread in 2 ways..one is by extending a thread and the other one is by implementing a runnbale interface..which one is better and why?

  22. Hi All,

    I have a simple question about this post. Let me take a scenario. I have used dynamic include. and my included file is keep changing its content. For this do i need to restart my server? lets say iam using tomcat 5.0 server.

    Thanks
    Motilal

  23. If it is dynamic include, the contents will be included automatically without restarting the server.

  24. I believe there is no difference between the two. the only reason we have both is to support multiple inheritance. ie, if our class is already extending from another class, we can create the thread only by implementing the runnable.

  25. Thank you Albin Joseph. I was finding the difference between jsp include and html include from business perspective and whether html include can accept parameters. And, Thank you M.Praveen and Garegin Chichyan also.

  26. I got the answer from this discussion. Now it is clear.

  27. @Vineet, you need to use dynamic include if you want to pass a parameter to the included file.

  28. His understanding on static and dynamic include are missing a little more facts.
    Please look into the following URL for clear understanding on Static and Dynamic include of pages.
    http://java.sun.com/products/jsp/tags/11/syntaxref1112.html

  29. Please look into the following URL for clear understanding on Static and Dynamic include of pages.
    http://java.sun.com/products/jsp/tags/11/syntaxref1112.html

Post a Comment