25

I am beginning to break apart a large JSP file into a some smaller JSP pages so I can reuse this across other areas of the site.

I can take the approach of leaving as a large Monolithic JSP file that takes params and adjusts it's behavior accordingly. The other approach I can take is breaking it apart so that it's called via jsp:include.

What is the performance concern in creating additional request calls that are dispatched from within the server? Is it better performance wise to keep it as one jsp page?

Shaun
  • 4,057
  • 7
  • 38
  • 48

4 Answers4

28

The jsp:include is a runtime directive unlike the <%@ include ... %> directive which happens to be a compile time directive (translation time, actually). A compile time include directive is relatively harmless since JSPs are usually precompiled for production, or in the worst case compiled for every deployment of the application. For this reason, it is preferable to use the compile time directive for static files, since the files are not bound to change at runtime.

The runtime include directive on the other head, if misused, can result in some performance hit, especially for static files being included. This is primarily because the JSP container would have to then fetch the contents of the static file and include them in the response. Hence, reserve the usage of the runtime directive for the really useful scenarios where another servlet or jsp's response is to be included in the response, and not merely to make the code look good.

Jama A.
  • 15,680
  • 10
  • 55
  • 88
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
19

You should be using:

<%@ include file="page.jsp" %>

This adds the content of page.jsp at translation time and there is no overhead.

<jsp:include page="page.jsp" />

Your approach adds the content at runtime and adds a lot of overhead.

Keith Adler
  • 20,880
  • 28
  • 119
  • 189
5

Static vs Dynamic include

The include directive, makes a copy of the included page and copies it into a JSP page (the "including page") during translation. This is known as a static include (or translate-time include) and uses the following syntax:

<%@ include file="/jsp/userinfopage.jsp" %>

The jsp:include action, dynamically includes output from the included page within the output of the including page, during runtime. This is known as a dynamic include (or runtime include) and uses the following syntax:

<jsp:include page="/jsp/userinfopage.jsp" flush="true" />

Performance Considerations

Static includes affect page size; dynamic includes affect processing overhead. Static includes avoid the overhead of the request dispatcher that a dynamic include necessitates, but may be problematic where large files are involved.

A dynamic include does increase processing overhead, with the necessity of the additional call to the request dispatcher.

http://docs.oracle.com/cd/A97336_01/buslog.102/a83726/keydev1.htm#1015959 http://docs.oracle.com/cd/A97336_01/buslog.102/a83726/genlovw3.htm

user2601995
  • 6,463
  • 8
  • 37
  • 41
0

Should not create a scenario to load lots of data to UI at a time, that will affect the performance whatever way you implement the jsp. Keep it simple. Understand , how much data a user probable to read in that specific scenario. Keep UIs userfriendly ,businessfriendly and techno friendly.

Calculate mow much static/dynamic contents are there and use include as per it's contexts. Use pagination with 10 - 50 records if you want to display records.Better use any framework to address the basic issues instead of addressing it from scratch.

if you don't need to use session in any jsp s make it as session false <%@ page session="false" %>

For include directive, JSP Engine adds the content of the inserted page at translation phase, so it does not have an impact on performance. For include action, JSP Engine adds the content of the inserted page at run time which imposes extra overhead.