Hi,
How do you define session object in a method. I have a method to track user count and count the times the user has visited the page in the current session.
p5.jsp
--------------------
... <body>
<%@ page import="javax.servlet.http.HttpSession,javax.servlet.http.*" %>
<%@ page session = "true" %>
Your session Id : <%=session.getId()%>
<%!
static class Inside_jspService {
public void ShowStuff(JspWriter out){
boolean has_visited = true;
try {
out.println(new java.util.Date()+"<BR>"
String userid = (String)session.getId();
out.println("<b>Your session ID is: "+ session.getId() + "</b><BR>"
// Try and get the count of users previously visited this application
Integer usercount = (Integer)session.getAttribute("USERCOUNT"
if ( usercount == null ) {
has_visited=false;
usercount = new Integer(usercount.intValue() + 1);
session.setAttribute("USERCOUNT", usercount);
}
else {
has_visited=true;
usercount++;
out.println("<b>Total number of users that have visited this Web Application "+ usercount + "</b><BR>"
session.setAttribute("USERCOUNT", usercount);
}
// Get the current count from the session
Integer count = (Integer)session.getAttribute("COUNT"
// If COUNT is not found, create it and add it to the session
if ( count == null ) {
count = new Integer(1);
session.setAttribute("COUNT", count);
}
else {
count = new Integer(count.intValue() + 1);
session.setAttribute("COUNT", count);
}
// Get the User's Name from the request
out.println("<b>You have visited this page: "+ count + " times during the current session.</b>"
}
catch (java.io.IOException e) {
}
}
}
%>
<%
Inside_jspService jp = new Inside_jspService();
jp.ShowStuff(out);
%>
</body>
..
---------------
I get errors on compilation:
Error: 500
Location: /lab05/p5.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\jakarta-tomcat-3.2.3\work\localhost_8080%2Flab05\_0002fp_00035_0002ejspp5_jsp_8.java:46: Undefined variable or class name: session
Integer count = (Integer)session.getAttribute("COUNT"
^
C:\jakarta-tomcat-3.2.3\work\localhost_8080%2Flab05\_0002fp_00035_0002ejspp5_jsp_8.java:50: Undefined variable or class name: session
session.setAttribute("COUNT", count);
^
C:\jakarta-tomcat-3.2.3\work\localhost_8080%2Flab05\_0002fp_00035_0002ejspp5_jsp_8.java:54: Undefined variable or class name: session
session.setAttribute("COUNT", count);
^
...
------------------
How do you define session object in a method. I have a method to track user count and count the times the user has visited the page in the current session.
p5.jsp
--------------------
... <body>
<%@ page import="javax.servlet.http.HttpSession,javax.servlet.http.*" %>
<%@ page session = "true" %>
Your session Id : <%=session.getId()%>
<%!
static class Inside_jspService {
public void ShowStuff(JspWriter out){
boolean has_visited = true;
try {
out.println(new java.util.Date()+"<BR>"
String userid = (String)session.getId();
out.println("<b>Your session ID is: "+ session.getId() + "</b><BR>"
// Try and get the count of users previously visited this application
Integer usercount = (Integer)session.getAttribute("USERCOUNT"
if ( usercount == null ) {
has_visited=false;
usercount = new Integer(usercount.intValue() + 1);
session.setAttribute("USERCOUNT", usercount);
}
else {
has_visited=true;
usercount++;
out.println("<b>Total number of users that have visited this Web Application "+ usercount + "</b><BR>"
session.setAttribute("USERCOUNT", usercount);
}
// Get the current count from the session
Integer count = (Integer)session.getAttribute("COUNT"
// If COUNT is not found, create it and add it to the session
if ( count == null ) {
count = new Integer(1);
session.setAttribute("COUNT", count);
}
else {
count = new Integer(count.intValue() + 1);
session.setAttribute("COUNT", count);
}
// Get the User's Name from the request
out.println("<b>You have visited this page: "+ count + " times during the current session.</b>"
}
catch (java.io.IOException e) {
}
}
}
%>
<%
Inside_jspService jp = new Inside_jspService();
jp.ShowStuff(out);
%>
</body>
..
---------------
I get errors on compilation:
Error: 500
Location: /lab05/p5.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\jakarta-tomcat-3.2.3\work\localhost_8080%2Flab05\_0002fp_00035_0002ejspp5_jsp_8.java:46: Undefined variable or class name: session
Integer count = (Integer)session.getAttribute("COUNT"
^
C:\jakarta-tomcat-3.2.3\work\localhost_8080%2Flab05\_0002fp_00035_0002ejspp5_jsp_8.java:50: Undefined variable or class name: session
session.setAttribute("COUNT", count);
^
C:\jakarta-tomcat-3.2.3\work\localhost_8080%2Flab05\_0002fp_00035_0002ejspp5_jsp_8.java:54: Undefined variable or class name: session
session.setAttribute("COUNT", count);
^
...
------------------