Following is an example to JSP Page Context Object.
indexpagecontext.html
[html]
<html>
<body>
<form action="pageContext.jsp">
Name: <input type="text" name="Name"/></br>
FullName:<input type="text" name="fullName"/></br></br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
[/html]
Here the developer just created two text boxes such as
Name and
FullName and also created submit button.
pageContext.jsp
[java]
<html>
<body>
<form action="getPageContext.jsp">
<%
String Name = request.getParameter("Name");
String fullName = request.getParameter("fullName");
out.println("Hello "+ Name+" ");
pageContext.setAttribute("Name", Name, PageContext.SESSION_SCOPE);
pageContext.setAttribute("fullName", fullName, PageContext.SESSION_SCOPE);
%>
<input type="submit" value="Click Here"/>
</form>
</body>
</html>
[/java]
request.getParameter() method is used to retrieve the details which are placed in HTML page.
pageContext.setAttribute() method is used to set the name of the attribute, attribute value and scope of that object. This method has no return type.
getPageContext.jsp
[java]
<html>
<body>
<%
String Name = (String) pageContext.getAttribute("Name", PageContext.SESSION_SCOPE);
String fullName = (String) pageContext.getAttribute("fullName", PageContext.SESSION_SCOPE);
out.println("Hai "+ Name+" ");
out.println("This is your fullname :"+fullName);
%>
</body>
</html>
[/java]
getAttribute(String AttributeName, int scope) method is used to get the attribute with specified scope.
Output
Output will be as follows with two text fields and one submit button.
After enter the text field names, it displays Hello followed by name then click on button that click here.
The final output will be as follows.