This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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 A.java 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 A.java file, B.jsp's code will not be put into the A.java, 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.
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.