View Javadoc

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 664175 2008-06-06 22:43:44Z djencks $
18   *
19   */
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.Date;
24  import java.util.Enumeration;
25  import java.util.ResourceBundle;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import util.HTMLFilter;
34  
35  /**
36   * Example servlet showing request headers
37   *
38   * @author James Duncan Davidson <duncan@eng.sun.com>
39   */
40  
41  public class SessionExample extends HttpServlet {
42  
43      ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
44  
45      public void doGet(HttpServletRequest request,
46                        HttpServletResponse response)
47              throws IOException, ServletException {
48          response.setContentType("text/html");
49  
50          PrintWriter out = response.getWriter();
51          out.println("<html>");
52          out.println("<body bgcolor=\"white\">");
53          out.println("<head>");
54  
55          String title = rb.getString("sessions.title");
56          out.println("<title>" + title + "</title>");
57          out.println("</head>");
58          out.println("<body>");
59  
60          // img stuff not req'd for source code html showing
61          // relative links everywhere!
62  
63          // XXX
64          // making these absolute till we work out the
65          // addition of a PathInfo issue 
66  
67          out.println("<a href=\"../sessions.html\">");
68          out.println("<img src=\"../images/code.gif\" height=24 " +
69                  "width=24 align=right border=0 alt=\"view code\"></a>");
70          out.println("<a href=\"../index.html\">");
71          out.println("<img src=\"../images/return.gif\" height=24 " +
72                  "width=24 align=right border=0 alt=\"return\"></a>");
73  
74          out.println("<h3>" + title + "</h3>");
75  
76          HttpSession session = request.getSession(true);
77          out.println(rb.getString("sessions.id") + " " + session.getId());
78          out.println("<br>");
79          out.println(rb.getString("sessions.created") + " ");
80          out.println(new Date(session.getCreationTime()) + "<br>");
81          out.println(rb.getString("sessions.lastaccessed") + " ");
82          out.println(new Date(session.getLastAccessedTime()));
83  
84          String dataName = request.getParameter("dataname");
85          String dataValue = request.getParameter("datavalue");
86          if (dataName != null && dataValue != null) {
87              session.setAttribute(dataName, dataValue);
88          }
89  
90          out.println("<P>");
91          out.println(rb.getString("sessions.data") + "<br>");
92          Enumeration names = session.getAttributeNames();
93          while (names.hasMoreElements()) {
94              String name = (String) names.nextElement();
95              String value = session.getAttribute(name).toString();
96              out.println(HTMLFilter.filter(name) + " = "
97                      + HTMLFilter.filter(value) + "<br>");
98          }
99  
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 }