KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SessionExample


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17 /* $Id: SessionExample.java 476311 2006-11-17 20:55:33Z vamsic007 $
18  *
19  */

20
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import util.HTMLFilter;
28
29 /**
30  * Example servlet showing request headers
31  *
32  * @author James Duncan Davidson <duncan@eng.sun.com>
33  */

34
35 public class SessionExample extends HttpServlet {
36
37     ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
38     
39     public void doGet(HttpServletRequest request,
40                       HttpServletResponse response)
41         throws IOException, ServletException
42     {
43         response.setContentType("text/html");
44
45         PrintWriter out = response.getWriter();
46         out.println("<html>");
47         out.println("<body bgcolor=\"white\">");
48         out.println("<head>");
49
50         String JavaDoc title = rb.getString("sessions.title");
51         out.println("<title>" + title + "</title>");
52         out.println("</head>");
53         out.println("<body>");
54
55         // img stuff not req'd for source code html showing
56
// relative links everywhere!
57

58         // XXX
59
// making these absolute till we work out the
60
// addition of a PathInfo issue
61

62         out.println("<a HREF=\"../sessions.html\">");
63         out.println("<img SRC=\"../images/code.gif\" height=24 " +
64                     "width=24 align=right border=0 alt=\"view code\"></a>");
65         out.println("<a HREF=\"../index.html\">");
66         out.println("<img SRC=\"../images/return.gif\" height=24 " +
67                     "width=24 align=right border=0 alt=\"return\"></a>");
68
69         out.println("<h3>" + title + "</h3>");
70
71         HttpSession session = request.getSession(true);
72         out.println(rb.getString("sessions.id") + " " + session.getId());
73         out.println("<br>");
74         out.println(rb.getString("sessions.created") + " ");
75         out.println(new Date(session.getCreationTime()) + "<br>");
76         out.println(rb.getString("sessions.lastaccessed") + " ");
77         out.println(new Date(session.getLastAccessedTime()));
78
79         String JavaDoc dataName = request.getParameter("dataname");
80         String JavaDoc dataValue = request.getParameter("datavalue");
81         if (dataName != null && dataValue != null) {
82             session.setAttribute(dataName, dataValue);
83         }
84
85         out.println("<P>");
86         out.println(rb.getString("sessions.data") + "<br>");
87         Enumeration names = session.getAttributeNames();
88         while (names.hasMoreElements()) {
89             String JavaDoc name = (String JavaDoc) names.nextElement();
90             String JavaDoc value = session.getAttribute(name).toString();
91             out.println(HTMLFilter.filter(name) + " = "
92                         + HTMLFilter.filter(value) + "<br>");
93         }
94
95         out.println("<P>");
96         out.print("<form action=\"");
97     out.print(response.encodeURL("SessionExample"));
98         out.print("\" ");
99         out.println("method=POST>");
100         out.println(rb.getString("sessions.dataname"));
101         out.println("<input type=text size=20 name=dataname>");
102         out.println("<br>");
103         out.println(rb.getString("sessions.datavalue"));
104         out.println("<input type=text size=20 name=datavalue>");
105         out.println("<br>");
106         out.println("<input type=submit>");
107         out.println("</form>");
108
109         out.println("<P>GET based form:<br>");
110         out.print("<form action=\"");
111     out.print(response.encodeURL("SessionExample"));
112         out.print("\" ");
113         out.println("method=GET>");
114         out.println(rb.getString("sessions.dataname"));
115         out.println("<input type=text size=20 name=dataname>");
116         out.println("<br>");
117         out.println(rb.getString("sessions.datavalue"));
118         out.println("<input type=text size=20 name=datavalue>");
119         out.println("<br>");
120         out.println("<input type=submit>");
121         out.println("</form>");
122
123         out.print("<p><a HREF=\"");
124     out.print(response.encodeURL("SessionExample?dataname=foo&datavalue=bar"));
125     out.println("\" >URL encoded </a>");
126     
127         out.println("</body>");
128         out.println("</html>");
129         
130         out.println("</body>");
131         out.println("</html>");
132     }
133
134     public void doPost(HttpServletRequest request,
135                       HttpServletResponse response)
136         throws IOException, ServletException
137     {
138         doGet(request, response);
139     }
140
141 }
142
Popular Tags