24 JSP Directives

Pravin Jain

epgp books

 

Introduction

 

In this module we will look into various directives which we can use in the jsp page. In the last module we had seen the various scripting elements. In that scripting elements if someone wanted to use the date, I wanted to give the current date and time. I might use expression which takes <%= new java.util.Date() %>, you just cant say <%= new Date() %>. We don’t have the import statement so we have to write the full name. This is just the scripting elements which we have seen so far. Those that goes in the scriptlet will be included in the _jspService method. Declaration get included in the class. Where do i apecify my imports. Because it is all content, while writing a servlet I can specify a content-type and say what type of content it is, jsp need not have all the time html content, it can be plain text content also. Many such things are specified using directives.

 

It can be specified using specific directive called the page  There are three directive in the jsp:

 

-page directive.

-include directive

-taglib directive

 

The page directive

 

The page directives controls how the jsp gets translated. Most commonly I would like to use certain things and not only the import statements. We would like to use classes from various packages and most common thing for that is the import statements. Since it is the jsp there is no way of writing import declarations. If you want import declarations to be included you need to mention the page directive.

 

As far as anything from java.lang package is concerned, we have automatic import available for all java pages. There are few other implicit imports which are always available to us. We don’t have to write import for javax.servlet, javax.servlet.http and javax.servlet.jsp. These three packages are always automatically included as an import declaration for any jsp which is generated. So, if we are trying to use classes or interfaces or anything from these three packages plus the java.lang package we don’t have to worry about. But trying to use anything else other than this we have to write an import. But you cant directly put an import. We have to put the page directive and directives are always written as <%@ page %> page is the name of the directive.

 

In our case, if we want to write the import we will write it as <%@ page import=’nameofthepackage’ %> here import is the attribute. If you have multiple packages you can separate them by comma(,). There are also other attributes available for the page directives:

 

language – this jsp file gets translated into what programming language and here the only option available is java default is also java so we can skip it.

 

We have buffer to specify buffer size in the jsp. so that buffer size for the jspWriter can be specified here.

 

buffer – the default here is 8k. it should always use quotes either single or double.

 

autoFlush – true or false again in quotes single or double. default value is true. If autoFlush is true, means, if the output generated exceeds the size of the buffer it should automatically get flushed.

If autoFlush is false, means, if the buffer gets full it will throw an exception.

session – true or false. default is true.

 

It means to participate in the session. It says I want to have the session object. If you don’t mention in the jsp the session attribute it considered as session=’true’, only if you mention session=’false’ it will not participate in a session. we will not even have the implicit object session available. So session attribute of page directive would specify whether we want to participate in the current session or not.

 

We might be building a page called error.jsp. It is used as target for an error. Whenever an error occurs it will be forwarded to this page. But while forwarding the error to the error.jsp there should be an exception object available. So if we are developing a page which is being used as an error page then we have an attribute called isErrorPage.

 

In this also two values are possible true or false. By default it is false. But if we develop an error page it will be set to true. When we are viewing the jsp there might be an occurrence of some exception in the jsp, then that is the time it should go to the error page. We also have an attribute called errorPage=”error.jsp”. In quotes it should specify the error page where if any error occur it should be forwarded.

 

In servlet we had a method called getServletInfo() where we were giving some copyright message and it was returning string. So if you are interested in having that information in your jsp then you have an attribute called info,

 

We have another attribute contentType. By default contentType is text/html. If you are writing some wml page for mobile or something it can be a different type of text which you are generating through a jsp. So if it is a different kind of text and not html you need to be mentioning by using contentType.

 

We also have another attribute called extends. For example in tomcat what we have observed is the translated java file which is being generated it is nothing but it is a class extending a HttpJspBase which is extending the HttpServlet which is available at the container. So all additional methods will be inherited plus it is an implementation of an HttpJspPage. This class which is used in tomcat have some base class being used which is an implementation of HttpJspPage which is used from where the translated java file would be inherited.

 

So we have our translated java class, they must have an implementation for HttpJspPage but when they implement this they extend from this base class. The HttpjJspBase in turn having an implementation of init, to invoke the jspInit method. It would have an implementation of destroy method to invoke the jspDestroy method. It would have an implementation of the service method to invoke the _jspService.

 

If you want to have some jspInit or jspDestroy method then those will be available by declaring. So use declaration in the jsp. In declaration we may use jspInit and jspDestroy and its implementation. _jspService is something done by the translator. So what should never happen in the declaration is _jspservice.

 

So whenever we look at the translated code it says something which extends from HttpJspBase. In case if we do not want to use this base class but would like to have the own implementation of the base class which should be used as the base. So in such a case a program you have got or may be specific to the project you have your own requirementation for all your jsps. Something additional being done you have developed your own base class. You do not want that translated class to be extending from the container but you would like to use the one which you have developed. In such a case you can use the page attribute called extends where you specify the class from where the translated java file would be extended.

 

We also have two more attributes for page directive:

 

pageEncoding – By default it is 8859-1 oe Latin-1. You can change it to UTF-8 or something else. When we look into internationalisation we may be interested in using character set or we may like to follow unicode, it is best to use  UTF-8.

 

isThreadSafe – true or false. By default it is true.

 

It can be specified as false and this can be indication to the container that it should be taking up the jsp. There are some instance variable used and if isThreadSafe is false that means it is not safe to use that variables in the multiple request being processed on the same servlet on the same jsp at the same time should not happen.

 

The include directive

 

Let us look at the another directive called include. We will use it as <%@ include %>. It has only one attribute which is file, which we like to include. Something similar to #include which we use in C language. What #include in C does? Let us say, we write a file and in this file we would like to include some other files. Some part of the code is in some other file and all other thing are in the same file.

 

We are developing some web application or some web site may be. In the web site we have some headers and footers being consistent all through out, the header part, the footer part, the code corresponding to the header, the code corresponding to the footer that kind of coding could be kept standard, so that if we modify something we don’t have to change in each and every page. So you could have them in separate files and if that is being done then in each and every jsp you can include the header jsp. We don’t use the extension jsp for the partial files those are not full jsp files. It is just part of the jsp. So to those files we normally give some other kind of extensions. The extension is upto us, but the convention being followed are .jspf, f for fragment, .inc – include file. so, you could have your own extension what ever you would like.  jspf is something which is preferable here.

 

You  create  jsp  and  in  each  and  every  jsp  you  would  like  to  go  for  include  <%@include

 

file=”header.jspf”%> and same at the bottom you would like to include footer say <%@ include file=”footer.jspf” %> . Include is done before the actual translation.

 

The taglib directive

 

If there is a jsp which is totally for presentation then it is mainly done by tags. jsp pages also has provision for using tags. There are tags which are by default available in any jsp. A jsp page can be using tag which can be developed by other programmers.

 

There are standard libraries also. So if you are using other tags in that case you will have to mention then there will be another file which is having an extension of tld, (tag library descriptor) which is again following an xml structure.

 

So if you have some such tld file, we can use because we are using tags here. In the jsp you have write taglib directive which specifies what is the tld file. Whenever you use the tag from that tld what will be the prefix for the tag which we are using. We have taglib directive with two attributes:

 

uri, which is used for specifying the location of the tld file and prefix,= which mentions whenever I use the tag from that tld what will be the prefix for the tag.

 

As a prefix code we cannot use jsp or jspx or java or javax as a prefix value. So prefix value cannot be any of these but it can be any other thing.

you can view video on JSP Directives

Suggested Reading:

  1. Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
  2. Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education
  3. Java Server Programming for Professionals by Ivan Bayross, Sh aranam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.
  4. http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html