• Post Reply Bookmark Topic Watch Topic
  • New Topic

Session Tracker  RSS feed

 
shishir gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am exploring session object in servlets. when I am using session's getAttribute() I get compile time error. Kindly let me know what's wrong in this code..
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionTracker extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
HttpSession session=req.getSession(true);
Integer count=(Integer)session.getAttribute("count");
if(count==null) {
count=new Integer(1);
}
else{
count=new Integer(count.intValue() +1);
}
session.setAttribute("count", count);
out.println("<html><head><title>Session Details</title></head>");
out.println("<body>You have visited this page" +count+((count.intValue()==1)? " time." : "times.") +"<br/>");
out.println(session.getId());
out.println(session.isNew());
out.println(new Date(session.getLastAccessedTime()));
out.println("</body></html>");

}
}
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shishir,
Looking quickly, it seems fine. What compiler error are you getting?
 
shishir gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i am compiling my source code I get the following errors:
D:\java\servlets>javac SessionTracker.java
SessionTracker.java:13: cannot resolve symbol
symbol : method getAttribute (java.lang.String)
location: interface javax.servlet.http.HttpSession
Integer count=(Integer)session.getAttribute("count");
^
SessionTracker.java:22: cannot resolve symbol
symbol : method setAttribute (java.lang.String,java.lang.Integer)
location: interface javax.servlet.http.HttpSession
session.setAttribute("count", count);
^
2 errors
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is working fine at my side.Check your compiler configuration.
 
shishir gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u pl tell me is there any class path tobe set? all my beans and other class files gets compiled but not this one. i am deploying my application on tomcat3.1 and JBoss3.2.3
 
TV KUMAR
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Tomcat 3.1 supports older version of Servlets which does not have setAttribute & getAttribute methods. Try using put and get methods instead
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!