Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined variable or class name: session

Status
Not open for further replies.

preethib

Programmer
Jul 20, 2000
39
IN
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=&quot;javax.servlet.http.HttpSession,javax.servlet.http.*&quot; %>
<%@ page session = &quot;true&quot; %>
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()+&quot;<BR>&quot;);
String userid = (String)session.getId();
out.println(&quot;<b>Your session ID is: &quot;+ session.getId() + &quot;</b><BR>&quot;);

// Try and get the count of users previously visited this application
Integer usercount = (Integer)session.getAttribute(&quot;USERCOUNT&quot;);
if ( usercount == null ) {
has_visited=false;
usercount = new Integer(usercount.intValue() + 1);
session.setAttribute(&quot;USERCOUNT&quot;, usercount);
}
else {
has_visited=true;
usercount++;
out.println(&quot;<b>Total number of users that have visited this Web Application &quot;+ usercount + &quot;</b><BR>&quot;);
session.setAttribute(&quot;USERCOUNT&quot;, usercount);
}

// Get the current count from the session
Integer count = (Integer)session.getAttribute(&quot;COUNT&quot;);
// If COUNT is not found, create it and add it to the session
if ( count == null ) {
count = new Integer(1);
session.setAttribute(&quot;COUNT&quot;, count);
}
else {
count = new Integer(count.intValue() + 1);
session.setAttribute(&quot;COUNT&quot;, count);
}
// Get the User's Name from the request
out.println(&quot;<b>You have visited this page: &quot;+ count + &quot; times during the current session.</b>&quot;);

}
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(&quot;COUNT&quot;);
^
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(&quot;COUNT&quot;, 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(&quot;COUNT&quot;, count);
^
...

------------------
 
The implicit variable session is not available in code blocks defined in <%! %> because this code actually resides outside of the compiled JSP's _jspService() method. To use the session you must have your method accept an HttpSession object.

Example:
<%!
public void test(HttpSession session) {
/* Do Whatever */
}
%>

<%
/* Call test Method */
test(session);
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top