Wednesday, 18 September 2013

JSP Directives

Lets talk about the JSP Directives.

Similart to other JSP Elements which we discussed earlier, jsp directive elements also have their own tags . ie.: <%@...%> so whatever you put in these tags will be considered as jsp directives.
These JSP Directives are certain preprocessing commands given to the JSP Engine. These commands should be processed before the jsp can be translated to a servlet.
So unlike the jsp elements you do not write java code or any kind of business logic in jsp directives
But you give some commands to the jsp engine.
Now we are going to see what are the commands, how do we implement them and what are the uses of it. We just said that we are using just providing commands to the jsp engine.

Why do we need JSP Directives?
To incorporate some additional features into the jsp so how the jsp basically behaves . When we say that incorporate some additional features into the jsp, mean that we modifying the servlet behaviour generated.
So we know that ultimately a jsp translated into a servlet. So jsp in term it does not have any meaning. When we are saying we are modying something jsp means we are actually changing certain behaviour in the servlet.
We can include some certain html files and we can provide tag libraries. We are going to see what tag libraries are and how do we use them?

Example: <%@ page import="java.util.ArrayList"%>

The above example is jsp page directive where Im importing the java.util.ArrayList class file.

Types of JSP Directives:

There are 3 different types in jsp directives: (

(1).  page directive:

Whatever you put in page directive is basically applied to the current jsp pages on which we are currently associated to
When it comes to directives the second element is also very important, so whatever you put the jsp directive tag is important as well. i.e :  <%@ page ......"%> //here page is 2nd element.
Here I put here "page" that means that it is page directive tell that what kind of element it is, that is here page directive element. So if I say percentage at the rate(@) page that means that this is the page directive.

Ex:
<%@ page import = "{IMPORTED_CLASSES}"
    contentType = "{CONTENT_TYPE}"
    isThreadSafe = "{true/false}"
    session = "{true/false}"
    buffer = "{BUFFER_SIZE}"
autoflush = "{true/false}"
extends = "{EXTENDS_FROM_PAGE}"
info = "{PAGE_INFO}"
errorPage = "{ERROR_PAGE_NAME}"
isErrorPage = "{true/false}"
language = "{LANGUAGE}"


We can provide number of predefined attributes values in a page directive and this is going to effect the page and the servlet at the end
These are the few of the attributes ,important attributes which we use in page directive elements
And we will see how it will effective the jsp page, after the application of this page directive:

1. So first thing is page import: so basically whenever you want to import certain classes files into your jsp you use the page directive import attribute. Lets say that you are working on some business logic in your servlet class and you are using packages and classes in the util package and some other classes. What do you do when you use them basically you import those classes whatever you are using in your file. Similarly we know that jsp ultimately converted into servlet and we do write  scriptlets in your jsps where we use certain other classes and utility packages as well. As a earlier example we saw probably have used ArrayList class in my jsp scriptlet when it is converted to a servlet, there is no error but it is probably going to give a error because Im using a ArrayList class but not imported that class, so how do we import? In the jsp we import by using the page directive:

(ii). contentType:

The default content Type would be html. Because most of the time whatever you sent back to the client is html page but you need to know that not only html but you can send back to the client bunch of different files that you can send like jar, image, video, zip and number of other file types which you can send to the client.
And the content type basically determines what type file you are sending back to the client.

(iii). isThreadSafe:

isThreadSafe ! What does it mean? we know that servlets are multithreaded environment, that means that only one servlet instance whenever new request comes in for the servlet it basically respond multiple threads and it is basically assume that bydefault your thread safe, it is thread safe because it is assume that you are writing your code in thread safe manner so that one particular change is made in one thread does not effect the other thread so there are any instance variables in threads in the servlets is trying to modify those variables so change made by one thread is visible to other thread in that case your servlet is not thread safe. So by default it is assumed that you implement some best practices in your code and you dont do such things and your servlets are thread safe but if you feel that your servlet is not thread safe, you would say that isThreadSafe is false, so if you dont want to make a thread safe is false and when you say that what the engine is going to do that engine is going to implement single thread module for your particular servelet which is generated by the jsp. It is basically is going to make the service() method synchronization that means only one thread can enter the service() method at one particular time.

4. session:

It determines for that you can use the current http session or  not? so by default it is true, and if you dont want to use the session, you can make it as false.

5. buffer, 6. autoflush, 7. extends,8. info are the attributes.: and another important attribute is errorPage, isErrorPage attribute.

When you say errorPage, you give some jsp page name( your html page name) whenever there is a error in current jsp page, it is going to take you to that particular error page defined the error page attribute. And how does it know that particular page is error page or not? if i say errorPage= ERROR.jsp, and when I write my error.jsp, i have to give page directive attribute isErrorPage and value of "true" but by default it is false so that means that every page is considered as a normal jsp page.
But if you want to make your jsp page an error page will have to say isErrorPage = true then it is treated as an error page.

So these are some of the attributes you can use in a jsp page directive and we just saw that how one application of these  directives we can change the behaviour of the generated servlet.


(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

(3.) taglib directive

This is the last and most important directive
Basially we provide certain tags in your jsp pages and implementing the taglib directive is going to help a lot because it is going to clean up your jsp page, it is going to look more pretty it is going to get a lot of loat of your shoulder as well.
So lot of common custom logic which you have in your application can be written in a taglib directive and you simply use the taglib directive in your jsp pages instead of again writing all those logic repeatedly in each of the page .

syntax: <%@ taglib uri="{TLD_FILE}" prefix="{PREFIX}"%>

It starts angular brackets percentage atTheRate and followed by taglib and we have uri and prefix, we will see what is really mean?
Components of taglib Libraries:

1. Tag handler class (2) Descriptor configuration file (3) taglib directive

1. Tag handler class
Now let's say that we have a common functionality used in each and every page in my application so what I do this, I put this common chunk of code in certain file and I will use this common code in multiple jsp pages.
So that common place where I'm puttin the common logic is the Tag Handler class. This is the place where I put the common custom logic and html generation.
And I use the taglib directive in my jsp page to invoke the class which is implementing the common logic now they have to be some bridge between the taglib directive and my jsp file and actual tag handler class so for that reason I have the Descriptor configuration file.
Descriptor configuration file is nothing but the property mapping file which has got information about the taglib directive and the tag handler class and what are the different property you can set for the tag libraries

overview of taglib directive:


jsp4.jpg
//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
<sharmanj:txtbox length="10" name="userName"/>
....

//sharmanj-taglib.tld
<tag>
<name>txtBox</name>
<tagclass>TextBoxTag</tagclass>
<bodycontent>EMPTY</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
....
</tag>


//web.xml
<web-app>
<taglib>
<taglib-uri>sharmanTags<taglib-uri>
<taglib-location>
sharmanj-taglib.tld
</taglib-location>
</taglib>
</web-app>

/*TxtBoxTag.java*/
public class TxtBoxTag extends TagSupport
{
   public int doStartTag()
   {
     ----
   }
 }

See here an example i will show all the different pages  classes and files involved in a tag library and how each file is connected to the other file

//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
......
<sharmanj:txtbox length="10" name="userName"/>
....

So first thing is my test.jsp, here What I did is? I have a common logic for displaying the text box so in my application whenever someone wants to use a text box in a jsp page they should not use the predefined text box present in html whatever input. But they have to use the jsp tag called sharmanj:txtbox to add a text box in their html or jsp file.
Lets say that I have provided some logic for that and that logic is present in the TxtBoxTag.java
We will have a look at the whole life cycle of tag handlers and all those things, what are the different tag handlers we can have? and whole life cycle of tag handlers and the next future class we will discuss tag libraries more details but here I just wanted to show you the different components we have placed tag libraries and how communication takes place?  between these components.
So my custom logic for the text box representation goes in the TxtBoxTag which is nothing but the tag handler class ( we just discussed).

/*TxtBoxTag.java*/
public class TxtBoxTag extends TagSupport
{
 public int doStartTag()
 {
  ----
 }
}

Lets say that I have that logic is placed in doStartTag() method
Now in my jsp I need to use the logic present in the TxtBoxTag.java to represent my text box.

//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
......
<sharmanj:txtbox length="10" name="userName"/>
....

So here I say I get the taglib directive and I say:" uri=sharmanjTags and prefix=sharmanj. When I say uri? it means that path to the tag libraries which I will be using and this path will be present in the web.xml, and which is the configuration for your servlets and jsps
web.xml is the configuration file for your servlet and jsps

//web.xml
<web-app>
<taglib>
<taglib-uri>sharmanTags<taglib-uri>
<taglib-location>
sharmanj-taglib.tld
</taglib-location>
</taglib>
</web-app>
So when I say taglib uri="sharmanj", What the container is going to do is? The Web Container finds the sharmanTags in the web.xml file under the <taglib> element
So web container is going to find the taglib uri with the name "sharmanjTags", so it finds a match now.
Once the web container finds the match in web.xml file
Now web container is going to see the <taglib-location>, so taglib-location is "sharmanj-taglib.tld"
Now web container goes to sharmanj-taglib.tld location which is a configuration file again where we provide certain different configurations for the tag libraries

//sharmanj-taglib.tld
<tag>
<name>txtBox</name>
<tagclass>TxtBoxTag</tagclass>
<bodycontent>EMPTY</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
....
</tag>
And within the sharmanj-taglibraries , I have different tag attributes I can use in this particular tablibraries i.e sharmanj-taglib.tld
And before that let me complete the text.jsp

//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
......
<sharmanj:txtbox length="10" name="userName"/>
....
I said that Im using the sharmanjTags lib and prefix can be anything example: sharmanj
So whatever I put prefix I should use that prefix certain feature in the taglib that is when I say prefix "sharmanj", Iam using prefix sharmanj on every element where I want to use the taglib. That is when I say sharmanj:txtbox, so here Iam trying to use  txtbox.
We know that taglib directive match to this particular location (i.e sharmanj-taglib.tld)  now Im going to use the "txtbox" taglib. Now it is going to search "txtbox" in the <name> tag in the file sharmanj-taglib.tld
Going to see what is the corresponding <tagclass> and it sees that tag class is "TxtBoxTag " and it is going to find that "TxtBoxTag" class , implement the basically call the methods in the TxtBoxTag.java

/*TxtBoxTag.java*/
public class TxtBoxTag extends TagSupport
{
 public int doStartTag()
 {
  ----
 }
}
So I put all my custom logic difining the TxtBox in my page.
So this is how the flow navigates in the different components and how your tag handler class is identified by the jsp pages .

Now finished the jsp taglibs

No comments:

Post a Comment