/Users/lyon/j4p/src/net/server/servlets/SessionExample.java

1    package net.server.servlets; 
2     
3    /* $Id: SessionExample.java,v 1.3 1999/04/10 05:22:10 duncan Exp $ 
4     * 
5     */ 
6     
7    import javax.servlet.ServletException; 
8    import javax.servlet.http.HttpServlet; 
9    import javax.servlet.http.HttpServletRequest; 
10   import javax.servlet.http.HttpServletResponse; 
11   import javax.servlet.http.HttpSession; 
12   import java.io.IOException; 
13   import java.io.PrintWriter; 
14   import java.util.Date; 
15   import java.util.ResourceBundle; 
16    
17    
18   /** 
19    * Example servlet showing request headers 
20    * 
21    * @author James Duncan Davidson <duncan@eng.sun.com> 
22    */ 
23    
24   public class SessionExample extends HttpServlet { 
25    
26       ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 
27    
28       public void doGet(HttpServletRequest request, 
29                         HttpServletResponse response) 
30               throws IOException, ServletException { 
31           response.setContentType("text/html"); 
32    
33           PrintWriter out = response.getWriter(); 
34           out.println("<html>"); 
35           out.println("<body bgcolor=\"white\">"); 
36           out.println("<head>"); 
37    
38           String title = rb.getString("sessions.title"); 
39           out.println("<title>" + title + "</title>"); 
40           out.println("</head>"); 
41           out.println("<body>"); 
42           out.println("<p>sw409 sessions</p>"); 
43    
44           // img stuff not req'd for source code html showing 
45           // relative links everywhere! 
46    
47           out.println("<a href=\"../servlets/sessions.html\">"); 
48           out.println("<img src=\"../images/code.gif\" height=24 " + 
49                   "width=24 align=right border=0 alt=\"view code\"></a>"); 
50           out.println("<a href=\"../servlets/index.html\">"); 
51           out.println("<img src=\"../images/return.gif\" height=24 " + 
52                   "width=24 align=right border=0 alt=\"return\"></a>"); 
53    
54           out.println("<h3>" + title + "</h3>"); 
55    
56           // How to get an HttpSession 
57    
58           HttpSession session = request.getSession(); 
59           out.println(rb.getString("sessions.id") + " " + session.getId()); 
60           out.println("<br>"); 
61           out.println(rb.getString("sessions.created") + " "); 
62           out.println(new Date(session.getCreationTime()) + "<br>"); 
63           out.println(rb.getString("sessions.lastaccessed") + " "); 
64           out.println(new Date(session.getLastAccessedTime())); 
65    
66           String dataName = request.getParameter("dataname"); 
67           String dataValue = request.getParameter("datavalue"); 
68           if (dataName != null && dataValue != null) { 
69               session.putValue(dataName, dataValue); 
70           } 
71    
72           out.println("<P>"); 
73           out.println(rb.getString("sessions.data") + "<br>"); 
74           String[] valueNames = session.getValueNames(); 
75           if (valueNames != null && valueNames.length > 0) { 
76               for (int i = 0; i < valueNames.length; i++) { 
77                   String name = valueNames[i]; 
78                   String value = session.getValue(name).toString(); 
79                   out.println(name + " = " + value + "<br>"); 
80               } 
81           } 
82    
83           out.println("<P>"); 
84           out.print("<form action=\""); 
85           out.print("SessionExample\" "); 
86           out.println("method=POST>"); 
87           out.println(rb.getString("sessions.dataname")); 
88           out.println("<input type=text size=20 name=dataname>"); 
89           out.println("<br>"); 
90           out.println(rb.getString("sessions.datavalue")); 
91           out.println("<input type=text size=20 name=datavalue>"); 
92           out.println("<br>"); 
93           out.println("<input type=submit>"); 
94           out.println("</form>"); 
95    
96           out.println("</body>"); 
97           out.println("</html>"); 
98    
99           out.println("</body>"); 
100          out.println("</html>"); 
101      } 
102   
103      public void doPost(HttpServletRequest request, 
104                         HttpServletResponse response) 
105              throws IOException, ServletException { 
106          doGet(request, response); 
107      } 
108   
109  } 
110