• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Session Tracker

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>");

}
}
 
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic