Overview of JSP Directives
JSP pages contain directives that direct the container about the processing of the page, these directives are associated with the compiled servlet that is automatically created by the JSP page. While the directives give processing instructions to the container running the component, the directive does not create any output
Standard Syntax for the directive is as follows:
<%@ directive attribute="value" %>
JSP contains three directives:
- Page Directive: To configure page level settings, example – import – “java.util.*”
- Include Directive: To include a file, example – file=” Header.JSP”
- Taglib Directive: Contains custom actions that can be used in the page.
<%@ taglib prefix="s"uri="/struts-tags" %>
Various JSP Directives
JSP directives are components of a JSP source code that guide the web container on how to convert the JSP page into its corresponding servlet. Let’s look at a few directives.
1. Page Directive
The page directive is used to instruct the JSP translator about certain aspects of the current JSP page like the content type to be used, like language in which page has to be written, etc.
The page directive has the following syntax:
<%@ page attribute 1="value 1" attribute 2="value 2" %>
Now let’s define a list of attributes used for the page directive:
a. Import: It is used to declare the java types to be used on the current page. Like if we want to use lists in the JSP page and iterate over them then we can import java.util.list, likewise other common packages like IO, util, etc can be imported in likewise way. There are certain implicit imports done, which we need not declare while working with the JSP pages and servlets in JSP Directives which are as follows:
b. Session: If set to value true, it indicates that the page will participate in the session management, the default value is true also, i.e. when you invoke the JSP page, javax.servlet.http.HttpSession instance will get created.
c. Buffer: It tells the buffer size of out implicit object in kb, it is necessary to mention kb at the end of mentioned buffer size, default value is 8kb or more depending on the JSP container, if this is set to none then it will cause the output to be written directly to the corresponding PrintWriter.
d. AutoFlush: The default value is true, this indicates that the buffer value shall be auto flushed when the buffer is full. A value of false indicates that the buffer is only flushed if the flush method of the response implicit object is called.
e. IsThreadSafe: It is a deprecated practice, not generally used, indicates the thread-safety implemented in the page.
f. Info: GetServletInfo method’s return value is specified here.
g. Errorpage: Incorporated for page error handling.
h. IsErrorPage: Tells whether a page can handle error or not.
i. ContentType: Whenever you send the data to the controller at the backend, the body has associated content type, like if you send JSON, XML, the plain text then the container will get aware of a content type that it shall respond with. The response object will be created likewise.
j. Page Encoding: Default, the value is ISO-8859-1, indicates the character encoding.
k. Language: Used to tell the scripting language used in a page, a default value will be java only.
l. Extends: Used to inherit the superclass like base layout can be inherited in all pages.
m. Trim Directive Whitespaces: Whether the template text has whitespaces or not, default is false.
2. Include Directive
If the content of one JSP Directives page has to be used in the other JSP then we need to incorporate the address of that JSP into it, the number of include statements will be equal to the number of pages you want to import into your current page. The advantage is that, you need not write the whole set of code from that page to this page, hence it prevents memory, time, complexity and overhead for developers when any change is supposed to be done.
You can import the header .jsp, footer.jsp, baseBodyLayout.jsp into all other pages and just you need to give the content to be rendered into the current page with the specific details adhering to the current page only.
Syntax for such inclusion is:
<%@ include file="url" %>
Please note that the merging of included files happens at the translation time only and not at request time, i.e.
- None of the included JSP code is executed; it is not even compiled yet.
- The files are first merged and then the entire merged output is translated as a unit.
- If the included files are ever changed, there is no general way for the container to know and recompile the entire translation unit.
3. Taglib Directive
It is used to tell the container which tag library a specific JSP requires. It is also used to assign a prefix that is used within the JSP page to identify tags from a specific tag library. Container when gets these taglibs, it locates the code for those taglibs and makes them ready to use JSP.
Syntax to use the taglib is as follows:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
So this makes an indication to the container that these all tags are used from this tag library and will be prefixed with the c: namespace within this JSP Directives. This URI can be associated with a tag library through a TLD file. The TLD can be mapped using a taglib map in the web.xml file, or via specific placement under the META-INF directory within a JAR archive.
The tag directory can also be specified as:
<%@ taglib prefix="wroxtags" tagdir="/WEB-INF/tags" %>
So you can place tag flies at WEB-INF/tags and container will get it from there.
Hence we have seen JSP Directives and what indications do the above-mentioned directives actually give to the container and how container keeps things resolved at the time of usage, these directives are used to add dynamic behavior and whenever dynamic web projects are designed these approaches come worthy. Likewise, the approach has been used in other synonymous frameworks like struts and many URL’s are available there for such usage.
This has been a guide to JSP Directives. Here we discuss the concept, various directives, and their explanation with examples. You can also go through our other suggested articles to learn more –