JSP pageContext

The object pageContext is used to access all the information of JSP page. It is an instance of javax.servlet.jsp.PageContext.

The scope of pageContext can be defined explicitly. The object is valid up to the specified scope only. Several layers in which pageContext can be accessed are as follows: -

  • SESSION_SCOPE
  • REQUEST_SCOPE
  • PAGE_SCOPE
  • APPLICATION_SCOPE

Methods of pageContext

The various methods of pageContext are as follows: -

  • void setAttribute(String attribute_name,Object attribute_value,int scope):- This method is used to set the attribute.
  • Object getAttribute(String attribute_name,int scope):- This method return the attribute of object type.
  • Object removeAttribute(String attribute_name,int scope):- This method is used to remove the attribute.
  • Object findAttribute(String attribute_name):- This method search the attribute passed within its bracket.

Example of pageContext

This is a simple example that defines the way of set and get the various attributes.

index.jsp

  Tutorials:
<% pageContext.setAttribute("Tutorial1","HTML",PageContext.SESSION_SCOPE);
 pageContext.setAttribute("Tutorial2","Servlet",PageContext.REQUEST_SCOPE);
 pageContext.setAttribute("Tutorial3","JSP",PageContext.PAGE_SCOPE);
 pageContext.setAttribute("Tutorial4","JavaScript",PageContext.APPLICATION_SCOPE);
 
String name1=(String)pageContext.getAttribute("Tutorial1",PageConteOutput xt.SESSION_SCOPE);
  
String name2=(String)pageContext.getAttribute("Tutorial2",PageContext.REQUEST_SCOPE);
 
String name3=(String)pageContext.getAttribute("Tutorial3",PageContext.PAGE_SCOPE);
 
String name4=(String)pageContext.getAttribute("Tutorial4",PageContext.APPLICATION_SCOPE);
 
out.println(name1); 

out.println(""+name2);
  
out.println(""+name3);
  
out.println(""+name4);  %>

Output:


Related Topics

JSP Implicit Objects

In JSP, implicit objects are pre-defined objects, created by the web container. These objects are created during the translation phase from JSP to Servlet. JSP allows you to call these...

1 minute read.

JSTL Function Tags

In JSTL, the purpose of function tags is similar to the various string methods in Java. Hence, these tag performs common string manipulation operations. Functional tag is represented by following URL:...

2 minutes read.

JSTL SQL Tags

In JSTL, SQL tags are used to access and manipulate various operations of database. It is responsible to interact the JSP page with any type of Database such as MySQL,...

2 minutes read.

JSP Taglib Directive

Taglib directive is used to define tag libraries in JSP page. It enables user to use custom tags within JSP file. A JSP page can contains more than one taglib...

1 minute read.

JSP application

JSP application object is an instance of servlet's ServletContext interface. It is used initialize parameter of one or more JSP pages in an application. Thus, the scope of application object...

1 minute read.

JSP session

In JSP, session object is used to establish a connection between a client and a server. It maintains the state between them so that server recognize the user easily. The session...

1 minute read.

JSTL Formatting Tags

In JSTL, the role of formatting tag is to specify the type of format that represents date and time. The following syntax represent the URL of formatting tag: - <%@ taglib...

1 minute read.

JSP Cookie Handling

Cookie is a small piece of information sent by server to recognize the user. This information is stored at client side in the textual form. In JSP, cookie is an object...

1 minute read.

JSP useBean, getProperty and setProperty tags

Java bean is a special type of Java class that contains private variables and public setter and getter methods. Java bean class implements Serializable interface and provides default constructor. JSP allows...

1 minute read.

JSP Comments

JSP provides separate tag for comment section. The content inside these tag is completely ignored by JSP engine.    The syntax of comment tag is mentioned below. <%-- This is a comment...

1 minute read.

JSP out

In JSP, out is an implicit object that is associated with the response object. This object is used to write content to the output stream. Methods of JSP out Following are...

1 minute read.

JSP Custom Tag

JSP custom tag provides flexibility to programmers, to declare their own tag. Thus, a user-defined tag can be created through custom tags to perform various operations. These custom tags behave as...

2 minutes read.

JSP Tutorial

Java Server Pages (JSP) is a server-side technology used to create static and dynamic web applications. The static content is expressed by text-based format files such as HTML, XML, SVG...

2 minutes read.

JSTL - Java Standard Tag Library

Java Standard tag library (JSTL) is a collection of various type of JSP tags. Each tag control the behavior of JSP page and perform specific task such as database access,...

1 minute read.

JSP Action Tags

As the name implies, JSP action tags are used to perform various actions during the execution of JSP page. Here, each action performs some specific task. These tasks include:  Forwarding the...

1 minute read.

JSP pageContext

The object pageContext is used to access all the information of JSP page. It is an instance of javax.servlet.jsp.PageContext. The scope of pageContext can be defined explicitly. The object is valid...

2 minutes read.

JSP Expression Language

JSP introduced the concept of expression language (EL) in JSP 2.0 version. The purpose of expression language is to access and manipulate data easily without using any type of scriptlet. Why...

1 minute read.

JSP config

JSP config object is used to send the configuration information to JSP page. Here, config is an instance of servlet's ServletConfig interface. In this case, the information is send to...

1 minute read.

JSP Request

The request object is used to fetch the user's data from the browser and pass this data to the server. The working of JSP request object is similar to the...

1 minute read.

JSP Include Directive

As the name implies, include directive is used to include the content of other files such as HTML, JSP, text into the current JSP page. These files are included during...

1 minute read.