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