| Author |
Session Tracker
|
shishir gupta
Greenhorn
Joined: Apr 30, 2004
Posts: 28
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Shishir, Looking quickly, it seems fine. What compiler error are you getting?
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
shishir gupta
Greenhorn
Joined: Apr 30, 2004
Posts: 28
|
|
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
Joined: Mar 18, 2004
Posts: 572
|
|
|
The code is working fine at my side.Check your compiler configuration.
|
 |
shishir gupta
Greenhorn
Joined: Apr 30, 2004
Posts: 28
|
|
|
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
Joined: Mar 18, 2004
Posts: 6
|
|
|
I think Tomcat 3.1 supports older version of Servlets which does not have setAttribute & getAttribute methods. Try using put and get methods instead
|
 |
 |
|
|
subject: Session Tracker
|
|
|