1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.*;
21 import java.text.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import util.HTMLFilter;
27
28 /**
29 * Example servlet showing request headers
30 *
31 * @author James Duncan Davidson <duncan@eng.sun.com>
32 */
33
34 public class SessionExample extends HttpServlet {
35
36 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
37
38 public void doGet(HttpServletRequest request,
39 HttpServletResponse response)
40 throws IOException, ServletException
41 {
42 response.setContentType("text/html");
43
44 PrintWriter out = response.getWriter();
45 out.println("<html>");
46 out.println("<body bgcolor=\"white\">");
47 out.println("<head>");
48
49 String title = rb.getString("sessions.title");
50 out.println("<title>" + title + "</title>");
51 out.println("</head>");
52 out.println("<body>");
53
54
55
56
57
58
59
60
61 out.println("<a href=\"../sessions.html\">");
62 out.println("<img src=\"../images/code.gif\" height=24 " +
63 "width=24 align=right border=0 alt=\"view code\"></a>");
64 out.println("<a href=\"../index.html\">");
65 out.println("<img src=\"../images/return.gif\" height=24 " +
66 "width=24 align=right border=0 alt=\"return\"></a>");
67
68 out.println("<h3>" + title + "</h3>");
69
70 HttpSession session = request.getSession(true);
71 out.println(rb.getString("sessions.id") + " " + session.getId());
72 out.println("<br>");
73 out.println(rb.getString("sessions.created") + " ");
74 out.println(new Date(session.getCreationTime()) + "<br>");
75 out.println(rb.getString("sessions.lastaccessed") + " ");
76 out.println(new Date(session.getLastAccessedTime()));
77
78 String dataName = request.getParameter("dataname");
79 String dataValue = request.getParameter("datavalue");
80 if (dataName != null && dataValue != null) {
81 session.setAttribute(dataName, dataValue);
82 }
83
84 out.println("<P>");
85 out.println(rb.getString("sessions.data") + "<br>");
86 Enumeration names = session.getAttributeNames();
87 while (names.hasMoreElements()) {
88 String name = (String) names.nextElement();
89 String value = session.getAttribute(name).toString();
90 out.println(HTMLFilter.filter(name) + " = "
91 + HTMLFilter.filter(value) + "<br>");
92 }
93
94 out.println("<P>");
95 out.print("<form action=\"");
96 out.print(response.encodeURL("SessionExample"));
97 out.print("\" ");
98 out.println("method=POST>");
99 out.println(rb.getString("sessions.dataname"));
100 out.println("<input type=text size=20 name=dataname>");
101 out.println("<br>");
102 out.println(rb.getString("sessions.datavalue"));
103 out.println("<input type=text size=20 name=datavalue>");
104 out.println("<br>");
105 out.println("<input type=submit>");
106 out.println("</form>");
107
108 out.println("<P>GET based form:<br>");
109 out.print("<form action=\"");
110 out.print(response.encodeURL("SessionExample"));
111 out.print("\" ");
112 out.println("method=GET>");
113 out.println(rb.getString("sessions.dataname"));
114 out.println("<input type=text size=20 name=dataname>");
115 out.println("<br>");
116 out.println(rb.getString("sessions.datavalue"));
117 out.println("<input type=text size=20 name=datavalue>");
118 out.println("<br>");
119 out.println("<input type=submit>");
120 out.println("</form>");
121
122 out.print("<p><a href=\"");
123 out.print(response.encodeURL("SessionExample?dataname=foo&datavalue=bar"));
124 out.println("\" >URL encoded </a>");
125
126 out.println("</body>");
127 out.println("</html>");
128
129 out.println("</body>");
130 out.println("</html>");
131 }
132
133 public void doPost(HttpServletRequest request,
134 HttpServletResponse response)
135 throws IOException, ServletException
136 {
137 doGet(request, response);
138 }
139
140 }