This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused by session.

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
While I was doing an exercise on session, I am confused by the description of some steps, could you help me?
1. Declare a servlet named SessionDisplayer, with a suitable servlet mapping. If
needed, refer to Chapter 2 to refresh yourself on <servlet> element setup.
Write the SessionDisplayer Servlet
2. Create a Java source file SessionDisplayer.java in /WEB-INF/classes or an
appropriate package directory. Write the class declaration in the source file,
extending HttpServlet.
3. Do the necessary preliminaries to obtain the reponse's PrintWriter, and set the
content type to "text/html."
4. Check for a parameter called getSession (request.getParameter
("getSession")). Most of the remaining steps should depend on the value of
getSession being "true."
5. Get hold of the session from the request.
6. If this is a new session, display the fact. Also, if the session is new, set up a
session attribute that records the number of accesses to the session, initialized to a
value of 1.
7. Increment this session attribute on every subsequent access to the session.
8. Display the number of times this session has been accessed, retrieving the
information from the session attribute.
9. Get the time the session was created. Display the session's age in minutes and
seconds by obtaining the current time and by working out the difference between
this and the session creation time.
Run the SessionDisplayer Servlet
10. Deploy and run the servlet, using a URL such as
11.
http://localhost:8080/ex0401/SessionDisplayer?getSession=true
12. Try recalling the servlet with the above URL, sometimes changing the getSession
parameter to a value of "false."

I don't know how to program according to step 6 and 7.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
6. HttpSession interface has a method called isNew
7. HttpSession interface has methods called setAttribute/getAttribute
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zhixiong,

Were you able to program the code using the hint given by Satou?
In case you are still facing any issues then I can provide you with the code that I tried to code with the points you had given and I get the output as desired.

Thanks,
Rancy
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancy,
Thanks for your advice and I have finished the code as following:
welcome for your scan.

But it seems unimaginable that even if I stop the server and restart, the access number still increase instead of starting count from scratch. Maybe I still not clear about the life cycle about session, or something else still confusing me.
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zhixiong
The code looks fine to me, although my implementation was little different. Here is a little information that I guess should clear your confustion.

But it seems unimaginable that even if I stop the server and restart, the access number still increase instead of starting count from scratch. Maybe I still not clear about the life cycle about session, or something else still confusing me.


After you stop and restart the server the count will not start from scratch and the access number will increase, the reason for this is: Session does not get invalidated if the container is shutdown and brought up again. I think the reason for this is, sessions are serialized to file system and then reloaded from file system on new request. Tomcat does not clear its cache of session data when it is restarted so it is still there.

Thanks,
-Rancy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic