File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Include directive Vs <jsp:include> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Include directive Vs <jsp:include>" Watch "Include directive Vs <jsp:include>" New topic

Include directive Vs <jsp:include>

Tresa P Anthony

Joined: Aug 14, 2006
Posts: 10

The JSP 2.0 spec (Section - JSP.5.4) says

The page attribute of both the jsp:include and the jsp:forward actions are interpreted relative to the current JSP page, while the file attribute in an include
directive is interpreted relative to the current JSP file.

An example is also given:

Consider the following four situations built using four JSP files: A.jsp, C.jsp, dir/B.jsp and dir/C.jsp:
• A.jsp says <jsp:include page=”dir/B.jsp”/> and dir/B.jsp says <%@ include file=”C.jsp” %>. In this case the relative specification C.jsp resolves to dir/C.jsp.
• A.jsp says <%@ include file=”dir/B.jsp”%> and dir/B.jsp says <jsp:include page=”C.jsp”/>. In this case the relative specification C.jsp resolves to C.jsp.

I am confused how the container figures out which C.jsp to include in these cases. Basically, whats the difference between
current JSP page and current JSP file
? Can someone please clarify?


Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
[Please Delete. Made an unclear explanation]

Tresa P Anthony

Joined: Aug 14, 2006
Posts: 10

Am surprised at the lukewarm response.

Anyways, the quoted text (including the examples) in my original post is from the JSP 2.0 spec.
Please explain how the container figures out the correct resource to include.

Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Tresa. Incase of <%@ include file=”dir/B.jsp”%> that is present in A.jsp, during translation phase(i.e, converting from .jsp to .java file) the JSP compiler goes to the 'dir' directive and finds the B.jsp, and place the file as it is in the A.jsp.(with <html><body>...</body></html>) Since B.jsp contains <jsp:include page=”C.jsp”/>, it will not be converted into .java file at this moment. Now the jsp compiler compiles into .class file. Then it goes for execution, while executing, the container sees <jsp:include page="C.jsp" />(actually it is a dynmaic one), then the jsp compiler converts from .jsp to .java file, and then converts into .class file, and executes it, and the response object of it is added to the A.jsp's response object, and the response is sent to the client.

Incase of <jsp:include page=”dir/B.jsp”/> that is present in A.jsp, while the jsp compiler compiles A.jsp into file, B.jsp's code will not be put into the, it just addresses <jsp:include page="dir/B.jsp" />(since it is a dynamic one). During executing A.class, the container sees <jsp:include page="dir/B.jsp" />, then the job is handover to the jsp compiler,the jsp compiler goes to the 'dir' directory and sees the B.jsp's code and in the code it sees <%@ include file=”C.jsp” %>, since it is a static one, it addes the C.jsp's code(with <html><body>...</body></html>) and converts it into .java file, and compiles into .class file and starts executing, at last the response object is added to the A.jsp's response object and sent to the client.

The above process will varies from one implementor to the another.

Here the current JSP page and current JSP means, the the file or page that is under compiling or executing.
Edgar Balderas

Joined: Jun 13, 2009
Posts: 14
Include directive appends the content of a jsp file to other. If you use <%@ include file="B.jsp"> inside A.jsp, the content of B.jsp will be added to A.jsp where it was called. Then, the compiler will compile them together as A.jsp.

Include action is equivalent of RequestDispatcher.include(ServletRequest, ServletResponse). In where you use <jsp:include page="B.jsp"> inside A.jsp, the processing will be delegated to B.jsp.

Static include occurs during translation time, so, works with JSP files which are going to be compiled and executed together.
Dynamic include occurs at running time (at requesting), so, works with compiled JSP pages which process requests and responses separately.

I agree. Here's the link:
subject: Include directive Vs <jsp:include>
It's not a secret anymore!