Implicit Objects in JSP, pageContext Implicit object in JSP, How to set,get and remove attributes to any scope in JSP


pageContext Implicit object in JSP, How to set,get and remove attributes to any scope in JSP

pageContext is an object of PageContext class, It is used to set, get and remove attribute with page, request, session and application scopes. we can set any value with setAttribute() method and also set the scope of the availability of the attribute in other pages.

Example of pageContext 

index.html

<html>
<body>
<form method="post" action="setName.jsp">
Enter Username<input type="text" name="userName">
<input type="submit" value="next">
</form>
</body>
</html>

setName.jsp

<html>
<body>
<%
String username=request.getParameter("userName");
out.write("Hello & Welcome "+username);
pageContext.setAttribute("username",username,pageContext.SESSION_SCOPE);

%>
<a href="welcomePage.jsp">next</a>
</body>
</html>

welcomePage.jsp

<html>
<body>
<%
String username=(String)pageContext.getAttribute("username",pageContext.SESSION_SCOPE);
%>
<h1>Hello <%=username%></h1>
</body>
</html>

In above example setName.jsp set the Attribute to pageContext with session scope and then get the Attribute in welcomePage.jsp because of session scope.

{ 0 comments... read them below or add one }

Post a Comment