Categories: JSPTutorial

JSP pageContext Object

<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">JSP <i><b>pageContext <&sol;b><&sol;i>Object is implicitly available to the developer on Java Server Pages&period; A <b><i>pageContext<&sol;i><&sol;b> implicit object is used for storing and retrieving page-related information and sharing objects within the same translation unit and same request&period; Also used as a convenience class that maintains a table of all the other implicit objects&period;<&sol;p>&NewLine;<p> <b>It stores referece to the implicit objects-<&sol;b><br &sol;>&NewLine;The following example shows how <i><b>PageContext <&sol;b><&sol;i>is used to populate other implicit objects&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public void &lowbar;jspService &lpar;HttpServletRequest request&comma; HttpServletResponse response&rpar;&NewLine; throws java&period;io&period;IOException&comma; ServletException &lbrace; &NewLine;&NewLine; &period;&period;&period; &NewLine; try &lbrace; &NewLine;&NewLine; &period;&period;&period; &NewLine; application &equals; pageContext&period;getServletContext &lpar;&rpar;&semi;&NewLine; config &equals; pageContext&period;getServletConfig &lpar;&rpar;&semi;&NewLine; session &equals; pageContext&period;getSession &lpar;&rpar;&semi;&NewLine; out &equals; pageContext&period;getOut &lpar;&rpar;&semi; &NewLine; &period;&period;&period; &NewLine;&NewLine; &rcub; catch &lpar;Throwable t&rpar; &lbrace;&NewLine; &period;&period;&period; &NewLine; &rcub; finally &lbrace;&NewLine; &period;&period;&period; &NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Provides convenience methods to get and set attributes in different scopes-<&sol;b><br &sol;>&NewLine;This example uses attributes to save and retrieve data in each of the four scopes&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&NewLine; &sol;&sol; Save data&NewLine; pageContext&period;setAttribute&lpar;"attr1"&comma; "value0"&rpar;&semi; &sol;&sol; PAGE&lowbar;SCOPE is the default&NewLine; pageContext&period;setAttribute&lpar;"attr2"&comma; "value1"&comma; PageContext&period;PAGE&lowbar;SCOPE&rpar;&semi;&NewLine; pageContext&period;setAttribute&lpar;"attr3"&comma; "value2"&comma; PageContext&period;REQUEST&lowbar;SCOPE&rpar;&semi;&NewLine; pageContext&period;setAttribute&lpar;"attr4"&comma; "value3"&comma; PageContext&period;SESSION&lowbar;SCOPE&rpar;&semi;&NewLine; pageContext&period;setAttribute&lpar;"attr5"&comma; "value4"&comma; PageContext&period;APPLICATION&lowbar;SCOPE&rpar;&semi;&NewLine;&percnt;&gt&semi;&NewLine; &NewLine;&lt&semi;&percnt;-- Show the values --&percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&equals; pageContext&period;getAttribute&lpar;"attr1"&rpar; &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&equals; pageContext&period;getAttribute&lpar;"attr2"&comma; PageContext&period;PAGE&lowbar;SCOPE&rpar; &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&equals; pageContext&period;getAttribute&lpar;"attr3"&comma; PageContext&period;REQUEST&lowbar;SCOPE&rpar; &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&equals; pageContext&period;getAttribute&lpar;"attr4"&comma; PageContext&period;SESSION&lowbar;SCOPE&rpar; &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&equals; pageContext&period;getAttribute&lpar;"attr5"&comma; PageContext&period;APPLICATION&lowbar;SCOPE&rpar; &percnt;&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>JSP <i>pageContext <&sol;i>Object Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page language&equals;"java" contentType&equals;"text&sol;html&semi; charset&equals;ISO-8859-1"&NewLine; pageEncoding&equals;"ISO-8859-1"&percnt;&gt&semi;&NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi;&NewLine;&lt&semi;html&gt&semi;&NewLine;&lt&semi;head&gt&semi;&NewLine;&lt&semi;title&gt&semi;JSP pageContext Object Example&lt&semi;&sol;title&gt&semi;&NewLine;&lt&semi;&sol;head&gt&semi;&NewLine;&lt&semi;body&gt&semi;&NewLine;&lt&semi;&percnt; &NewLine; JspWriter pw &equals; pageContext&period;getOut&lpar;&rpar;&semi;&NewLine; pw&period;print&lpar;"Hello Dinesh&period;&period;&period;&period;This is another example of JSP 'pageContext' object"&rpar;&semi;&NewLine;&percnt;&gt&semi;&NewLine;&lt&semi;&sol;body&gt&semi;&NewLine;&lt&semi;&sol;html&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>As you can see above&comma; using JSP <b><i>pageContext <&sol;i><&sol;b>object&comma; <i><b>JspWriter <&sol;b><&sol;i>instance is created&period; Using this <i><b>JspWriter<&sol;b><&sol;i> object&comma; String response is set that displayed on the browser when JSP served a request&period;<&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;page-implicit-object&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;jsp-scopes-example&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;page-implicit-object&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;jsp-scopes-example&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; '326'&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