Categories: JSPTutorial

JSP Page Directive

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;" trbidi&equals;"on">Page Directives in JSPs are instruction from JSPs to JSP Engine&period; The page directive is used to provide instructions to the container that pertain to the current JSP page&period; You may code page directives anywhere in your JSP page&period; By convention&comma; page directives are coded at the top of the JSP page&period;<&sol;p>&NewLine;<p> General syntex of JSP Page Directives generally look like this&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page attribute1&equals;"value1" attribute2&equals;"value2" &percnt;&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Syntex&colon; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page import&equals;"xyz" language&equals;"xyz" extends&equals;"xyz" session&equals;"xyz" buffer&equals;"xyz" isThreadSafe&equals;"xyz" autoFlush&equals;"xyz" errorPage&equals;"xyz" info&equals;"xyz" contentType&equals;"xyz" isErrorPage&equals;"xyz" &percnt;&gt&semi;<&sol;pre>&NewLine;<p>&NewLine;<b>Example&colon;<&sol;b> <&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page import&equals;"java&period;util&period;&ast;" &percnt;&gt&semi;<&sol;pre>&NewLine;<p>&NewLine;<b>Explanation&colon;<&sol;b> As shown above&comma; Page directives has many attributes&period; Page Directive can be used multiple times in a single JSP&period; However&comma; except import attribute&comma; no other attirbute can be used multiple times&period;<&sol;p>&NewLine;<p> <b>language Attribute&colon;<&sol;b> At present JSP supports only JAVA as a language attirbute&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page language&equals;"java" &percnt;&gt&semi;<&sol;pre>&NewLine;<p>&NewLine;<b>import Attribute&colon;<&sol;b> import Attribute in Page Directive allows to import JAVA packages to JSP&period; You can import more than one package as comma seperated values or you can write multiple Page Directive each having its own import attribute&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page import&equals;"java&period;util&period;&ast;" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>multiple import<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page import&equals;"java&period;util&period;&ast;&comma;java&period;sql&period;&ast;" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>extends Attribute&colon;<&sol;b> extends Attribute allows to extend Super Class in JSP&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page extends&equals;"com&period;dineshonjava&period;jsp&period;SomeClass" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>s<&sol;b><b>ession Attribute&colon; <&sol;b>session Attribute can have either true or false value&period; Default value is always true&period; If it is set to false&comma; session object can not be used in JSP&period;<br &sol;>&NewLine;he session attribute indicates whether or not the JSP page uses HTTP sessions&period; A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object&period;<&sol;p>&NewLine;<p>Following directive allows the JSP page to use any of the builtin object session methods such as session&period;getCreationTime&lpar;&rpar; or session&period;getLastAccessTime&lpar;&rpar;&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page session&equals;"true" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>buffer Attribute&colon; <&sol;b>You can specify either none or some value in kb&period; Default value is 8kb&period; This value is used by implicit object &&num;8220&semi;out&&num;8221&semi; to buffer output from compiled JSP to client&&num;8217&semi;s web browser&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page buffer&equals;"none" &percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>autoFlush Attribute&colon;<&sol;b> autoFlush Attribute in JSP has either true or false value&period; Default value is set to True&period; That means when buffer overflows&comma; it automatically flush the buffer&period; If it is set to false&comma; JSP will throw an error when buffer overflows&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page autoFlush&equals;"false" &percnt;&gt&semi;<&sol;pre>&NewLine;<p>This directive causes the servlet to flush the output buffer when full&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page autoFlush&equals;"true"&percnt;&gt&semi;<&sol;pre>&NewLine;<p>Usually&comma; the buffer and autoFlush attributes are coded on a single page directive as follows&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page buffer&equals;"16kb" autoflush&equals;"true"&percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>isErrorPage Attribute&colon;<&sol;b> isErrorPage Attribute in Page Directive has either true or false value&period; Default value is set to false&period; Implicit object exception can not be used if isErrorPage value is false&period; In order to use exception object in JSP&comma; isErrorPage attribute value must be set to true&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page isErrorPage&equals;"true"&percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>errorPage Attribute&colon; <&sol;b>errorPage Attribute in Page Directive points to the page which handles exception thrown in current jsp&period; Location of the errorPage jsp is shown using relative path&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page errorPage&equals;"customErrorPage&period;jsp"&percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>contentType Attribute&colon; <&sol;b>contentType Attribute in Page Directive suggest the MIME Type and Character Encoding for JSP&period; Default value for MIME Type is text&sol;html and default value for Character Encoding is ISO-8859-1&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page contentType&equals;"text&sol;html"&percnt;&gt&semi;<&sol;pre>&NewLine;<p>The following directive sets the content type as a Microsoft Word document&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page contentType&equals;"application&sol;msword"&percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>isThreadSafe Attribute&colon;<&sol;b> isThreadSafe Attribute in Page Directive has either true or false value&period; Default value is set to true&period; That means&comma; JSP engine sends mutiple requests to JSP same time&period; Code inside JSP must be synchronized to handle concurrent reqests&period; If the value of the attribute is set to false&comma; JSP Engine will send only one reqest at a time to JSP&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page isThreadSafe&equals;"false"&percnt;&gt&semi;<&sol;pre>&NewLine;<p><b>info Attribute&colon; <&sol;b>info Attribute in Page Directive is just a text String&period; <&sol;div>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page info&equals;"This is JSP Page"&percnt;&gt&semi;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;09&sol;jsp-directives&period;html">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;08&sol;jsp-tutorial-baby-step-to-server-pages&period;html">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;09&sol;taglib-directive-in-jsp&period;html">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jsp-directives&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;taglib-directive-in-jsp&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '345'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago