/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.servlet.http.*; 

2. import javax.servlet.*; 

3. import java.io.*; 

4. import java.util.*; 

5.  

6. public class ServSes  extends HttpServlet 

7.

8.   public void 

9.   doGet ( HttpServletRequest req, HttpServletResponse resp ) 

10.     throws ServletException, IOException 

11.   { 

12.     HttpSession ses = req.getSession(true); 

13.  

14.     resp.setContentType ( "text/html" ) ; 

15.  

16.     PrintWriter out = resp.getWriter(); 

17.  

18.     out.println ( "<HTML>" ) ; 

19.     out.println ( "<HEAD>" ) ; 

20.  

21.     out.println ( "<TITLE>" ) ; 

22.     out.println ( "Session servlet" ) ; 

23.     out.println ( "</TITLE>" ) ; 

24.  

25.     out.println ( "</HEAD>" ) ; 

26.  

27.     out.println ( "<BODY>" ) ; 

28.     out.println ( "<H1>Session servlet</H1>" ) ; 

29.  

30.     if (ses != null

31.     { 

32.       out.println ( "<UL>" ) ; 

33.  

34.       out.println ( "<LI>" + "id == " +  ses.getId()  ) ; 

35.  

36.       Enumeration  nms = ses.getAttributeNames(); 

37.       String value = ""

38.  

39.       while ( nms.hasMoreElements() ) 

40.       { 

41.         String nm = (String) nms.nextElement() ; 

42.         out.println ( "<LI>" + "name == " + nm ) ; 

43.         value = (String) ses.getAttribute( nm ) ; 

44.         out.println ( "<LI>" + "value == " + value ) ; 

45.       } 

46.  

47.       out.println ( "<LI>" + "creatime == " ) ; 

48.       out.println (  ses.getCreationTime( ) ) ; 

49.       out.println ( "<LI>" + "lasttime == " ) ; 

50.       out.println (  ses.getLastAccessedTime( ) ) ; 

51.  

52.       out.println ( "</UL>" ) ; 

53.  

54.       String urlthisservlet = req.getRequestURI() ; 

55.       String urlplus = resp.encodeURL( urlthisservlet ) ; 

56.  

57.       String tmp = 

58.       "<A HREF=\"" + urlplus + "\">" + "Link to this servlet</A>"

59.       out.println ( tmp ) ; 

60.  

61.       ses.setAttribute ( "somename", value + " -joy- " ); 

62.     } 

63.  

64.     out.println ( "</BODY>" ) ; 

65.     out.println ( "</HTML>" ) ; 

66.  

67.     out.flush (); 

68.     out.close (); 

69.   } 

70.

71. ======== ( miscellaneous ) ================== 

72. ses.setMaxInactiveInterval ( 300 ) ; 

73. ======== 

74. ses.invalidate();