• Post Reply Bookmark Topic Watch Topic
  • New Topic

Page Scope clarification

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learnt that page scope will work only within the specified page but when i click the other link the page scope has no effect it show the name attribute in next page also. Please help me out.

1st jsp page

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body bgcolor="Lightblue">
<%HttpSession ses = request.getSession();
String user = (String)ses.getAttribute("name");
%>

Welcome to Scopes Page <br>
<% out.println("Hello" +user);%>
<a href ="jsp-page2.jsp"> Click to check the page scope </a>

<c:set var="name" value="karthi" scope="page" />
Local Variable: <c:out value="${name}" />


</body>
</html>


2 nd jsp page


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>

</head>

<body>
The other page variable <c:out value = "${name}"/> <br>
<a href="Jsp3.jsp"> Click here to test the working of page scope</a>
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A page scoped variable is valid only within the page within which it is created. It cannot be carried to another page. If you want a scoped variable to be available in the next request, it should be placed in the session (and properly cleaned up when no loner needed).
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

Ok,I understood what you said but i have used page scope in my programs but i still find the scope is availabel to next page
i am getting the output for name variable in next page
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Pandian wrote:but i still find the scope is availabel to next page

It is not. The next page is picking up the name attribute that is in session scope, not your page-scoped variable of the same name (bad practice for just this reason).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!