Wednesday 18 September 2013

Directive: page directive:

(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.

No comments:

Post a Comment