001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 /* $Id: SessionExample.java 514091 2007-03-03 06:26:39Z jdillon $
018 *
019 */
020
021 import java.io.*;
022 import java.util.*;
023 import javax.servlet.*;
024 import javax.servlet.http.*;
025
026 import util.HTMLFilter;
027
028 /**
029 * Example servlet showing request headers
030 *
031 * @author James Duncan Davidson <duncan@eng.sun.com>
032 */
033
034 public class SessionExample extends HttpServlet {
035
036 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
037
038 public void doGet(HttpServletRequest request,
039 HttpServletResponse response)
040 throws IOException, ServletException
041 {
042 response.setContentType("text/html");
043
044 PrintWriter out = response.getWriter();
045 out.println("<html>");
046 out.println("<body bgcolor=\"white\">");
047 out.println("<head>");
048
049 String title = rb.getString("sessions.title");
050 out.println("<title>" + title + "</title>");
051 out.println("</head>");
052 out.println("<body>");
053
054 // img stuff not req'd for source code html showing
055 // relative links everywhere!
056
057 // XXX
058 // making these absolute till we work out the
059 // addition of a PathInfo issue
060
061 out.println("<a href=\"../sessions.html\">");
062 out.println("<img src=\"../images/code.gif\" height=24 " +
063 "width=24 align=right border=0 alt=\"view code\"></a>");
064 out.println("<a href=\"../index.html\">");
065 out.println("<img src=\"../images/return.gif\" height=24 " +
066 "width=24 align=right border=0 alt=\"return\"></a>");
067
068 out.println("<h3>" + title + "</h3>");
069
070 HttpSession session = request.getSession(true);
071 out.println(rb.getString("sessions.id") + " " + session.getId());
072 out.println("<br>");
073 out.println(rb.getString("sessions.created") + " ");
074 out.println(new Date(session.getCreationTime()) + "<br>");
075 out.println(rb.getString("sessions.lastaccessed") + " ");
076 out.println(new Date(session.getLastAccessedTime()));
077
078 String dataName = request.getParameter("dataname");
079 String dataValue = request.getParameter("datavalue");
080 if (dataName != null && dataValue != null) {
081 session.setAttribute(dataName, dataValue);
082 }
083
084 out.println("<P>");
085 out.println(rb.getString("sessions.data") + "<br>");
086 Enumeration names = session.getAttributeNames();
087 while (names.hasMoreElements()) {
088 String name = (String) names.nextElement();
089 String value = session.getAttribute(name).toString();
090 out.println(HTMLFilter.filter(name) + " = "
091 + HTMLFilter.filter(value) + "<br>");
092 }
093
094 out.println("<P>");
095 out.print("<form action=\"");
096 out.print(response.encodeURL("SessionExample"));
097 out.print("\" ");
098 out.println("method=POST>");
099 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 }