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 664175 2008-06-06 22:43:44Z djencks $
018     *
019     */
020    
021    import java.io.IOException;
022    import java.io.PrintWriter;
023    import java.util.Date;
024    import java.util.Enumeration;
025    import java.util.ResourceBundle;
026    
027    import javax.servlet.ServletException;
028    import javax.servlet.http.HttpServlet;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    import javax.servlet.http.HttpSession;
032    
033    import util.HTMLFilter;
034    
035    /**
036     * Example servlet showing request headers
037     *
038     * @author James Duncan Davidson <duncan@eng.sun.com>
039     */
040    
041    public class SessionExample extends HttpServlet {
042    
043        ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
044    
045        public void doGet(HttpServletRequest request,
046                          HttpServletResponse response)
047                throws IOException, ServletException {
048            response.setContentType("text/html");
049    
050            PrintWriter out = response.getWriter();
051            out.println("<html>");
052            out.println("<body bgcolor=\"white\">");
053            out.println("<head>");
054    
055            String title = rb.getString("sessions.title");
056            out.println("<title>" + title + "</title>");
057            out.println("</head>");
058            out.println("<body>");
059    
060            // img stuff not req'd for source code html showing
061            // relative links everywhere!
062    
063            // XXX
064            // making these absolute till we work out the
065            // addition of a PathInfo issue 
066    
067            out.println("<a href=\"../sessions.html\">");
068            out.println("<img src=\"../images/code.gif\" height=24 " +
069                    "width=24 align=right border=0 alt=\"view code\"></a>");
070            out.println("<a href=\"../index.html\">");
071            out.println("<img src=\"../images/return.gif\" height=24 " +
072                    "width=24 align=right border=0 alt=\"return\"></a>");
073    
074            out.println("<h3>" + title + "</h3>");
075    
076            HttpSession session = request.getSession(true);
077            out.println(rb.getString("sessions.id") + " " + session.getId());
078            out.println("<br>");
079            out.println(rb.getString("sessions.created") + " ");
080            out.println(new Date(session.getCreationTime()) + "<br>");
081            out.println(rb.getString("sessions.lastaccessed") + " ");
082            out.println(new Date(session.getLastAccessedTime()));
083    
084            String dataName = request.getParameter("dataname");
085            String dataValue = request.getParameter("datavalue");
086            if (dataName != null && dataValue != null) {
087                session.setAttribute(dataName, dataValue);
088            }
089    
090            out.println("<P>");
091            out.println(rb.getString("sessions.data") + "<br>");
092            Enumeration names = session.getAttributeNames();
093            while (names.hasMoreElements()) {
094                String name = (String) names.nextElement();
095                String value = session.getAttribute(name).toString();
096                out.println(HTMLFilter.filter(name) + " = "
097                        + HTMLFilter.filter(value) + "<br>");
098            }
099    
100            out.println("<P>");
101            out.print("<form action=\"");
102            out.print(response.encodeURL("SessionExample"));
103            out.print("\" ");
104            out.println("method=POST>");
105            out.println(rb.getString("sessions.dataname"));
106            out.println("<input type=text size=20 name=dataname>");
107            out.println("<br>");
108            out.println(rb.getString("sessions.datavalue"));
109            out.println("<input type=text size=20 name=datavalue>");
110            out.println("<br>");
111            out.println("<input type=submit>");
112            out.println("</form>");
113    
114            out.println("<P>GET based form:<br>");
115            out.print("<form action=\"");
116            out.print(response.encodeURL("SessionExample"));
117            out.print("\" ");
118            out.println("method=GET>");
119            out.println(rb.getString("sessions.dataname"));
120            out.println("<input type=text size=20 name=dataname>");
121            out.println("<br>");
122            out.println(rb.getString("sessions.datavalue"));
123            out.println("<input type=text size=20 name=datavalue>");
124            out.println("<br>");
125            out.println("<input type=submit>");
126            out.println("</form>");
127    
128            out.print("<p><a href=\"");
129            out.print(response.encodeURL("SessionExample?dataname=foo&datavalue=bar"));
130            out.println("\" >URL encoded </a>");
131    
132            out.println("</body>");
133            out.println("</html>");
134    
135            out.println("</body>");
136            out.println("</html>");
137        }
138    
139        public void doPost(HttpServletRequest request,
140                           HttpServletResponse response)
141                throws IOException, ServletException {
142            doGet(request, response);
143        }
144    
145    }