• Post Reply Bookmark Topic Watch Topic
  • New Topic

why not session.setAttribute working ?

 
Ghazala Islam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to display count of page hit through session but every time its displaying s1=0,either its not setting the attribute of session or what?
The code is given below :
<%@ page import = "java.io.*,
java.util.*,
java.sql.*"
session="true"
%>
<%! int a; %>
<%
Integer s1 = (Integer)session.getAttribute("sess");
out.println("s1 before " + s1);
if (s1==null) {
s1 = new Integer(0);
}
else {
s1 = new Integer(s1.intValue()+1);
}
session.setAttribute("sess",s1);
out.println("s1 after " +s1);

%>
<html>
<body> No of times you request for the page !</body></html>
 
Mandy S Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is displaying what you want. This is how your code output looks like.
s1 before null s1 after 0 No of times you request for the page !
s1 before 1 s1 after 2 No of times you request for the page !
s1 before 2 s1 after 3 No of times you request for the page !
s1 before 3 s1 after 4 No of times you request for the page !
------- ,,-----------
If you keep hitting refresh button, it's changing its value.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I ran your JSP on Tomcat 3.2 and it's working fine, just as Mandy described.
-anthony
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!