.


Divya

JSP pageContext Implicit Object with Example


In JSP, pageContext is an instance of javax.servlet.jsp.PageContext.

Using this object you can set,get or remove attribute from one of the following scopes:

  • JSP Page Scope: PAGE_CONTEXT
  • HTTP Request Scope: REQUEST_CONTEXT
  • HTTP Session Scope: SESSION_CONTEXT
  • Application Level Scope: APPLICATION_CONTEXT

Example of pageContext Implicit Object

index.html

<!DOCTYPE html>
<html>
<head>
<title>pageContext Implicit Object</title>
</head>
<body>
<form action="process.jsp">
<input type="text" placeholder="first name" name="userid"> <br><br>
<input type="text" placeholder="last name" name="password"> <br> <br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>

process.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP PagContext </title>
</head>
<body>
<%
String userid=request.getParameter("userid");
String password=request.getParameter("password");
out.println("Hello :"+userid);
pageContext.setAttribute("UserName", userid, PageContext.SESSION_SCOPE);
pageContext.setAttribute("UserPassword", password, PageContext.SESSION_SCOPE);
%>
</body>
</html>

display.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP PagContext </title>
</head>
<body>
<%
String username= (String) pageContext.getAttribute("UserName", PageContext.SESSION_SCOPE);
String userpassword= (String) pageContext.getAttribute("UserPassword", PageContext.SESSION_SCOPE);
out.println("Hi "+username+" you enter");
out.println("<br><br>");
out.println("Userid :"+username);
out.println("<br>");
out.println("Password :"+userpassword);
%>
</body>
</html>

Output

Pagecontext implicit JSP Pagecontext Pagecontext

.