Wednesday 18 September 2013

(2) include directive

(2) include directive

Fig: jsp3.jpg

Similarly the include directive will have include as the name in the 2nd element after the tags i.e: <%@ include file=....%.>
Can we say include file= "whatever file you want to include in this page. Lets take an example
we know that in any web applications there will be a number of different pages, for example i have 100 pages in my web applications and often times there are certain common areas in every page. Those common areas would be headers and footers regions so most of the times I will have the same header and same footer in all the pages.
And writing the code for header and footer in each and every page in order best practice because if you want to change the header so most of the times I have the same header or footer text anything in the header or footer you will basically have make changes each and every page which is not a best thing to do so what you basically do is you write a separate header ( ex: Hello<=userName%> )and footer page (Example:
<table><tr>
<td>Contact Details</td>
<tr></table>

    And you put whatever text you want to in the header and footer and if at all if you want to make any changes in the header or footer you simply change the  header.jsp or footer.jsp you have done, and you are only make change at one place as post to making changes in 100 places. And what you do then you add this header and footer pages in every page you want to make use of it so you basically include these header and footer pages and iyou can include these pages using the inlcude directive so this definately is very effective and it saves a lot of time for us.
One thing to note here is the inlcudsion of header and footer files or inclusion of the soft files into the main file is done at the translation time. So what is this translation time? time where your jsp translated to a servlet.
So the first time I write my Main page add the header and footer files i.e <%@ include file=header.jsp> , <%@ include file=footer.jsp>.  and I compile it now it is going to translate the time of call it is going to translate my jsp pages to a servlet page , and my servlet is going to be loaded into the memory.
But let's say that one issue here that could occur this let us say that I included my header and footer to my main page and I rended the page now later if I had to change the header.jsp file , initially it was "Hello.userName", and I want to change it into "HI. userName" so I simply modify the header .jsp and save it. Will this effect my main page? Will I see "Hi userName " here in main page? No , because my main page was translated into a servlet even before the header.jsp made its change.
So whatever changes made in header.jsp, that is not reflected my main page so if I want that change reflected to my main page , what I need to do is? again need to recompile my main page so that it is going to take whatever it is latest from the subpages that is header.jsp or footer.jsp and then it is going to be translated into servlet again then render that the user where I will see " Hi. userName"
So this is the jsp inlcude directive


No comments:

Post a Comment