blob: cca91e0f5ef88957d327aecfd9bb2f93af2904eb [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
刘洪青6266f992017-05-15 21:21:03 +080018import java.io.IOException;
19import java.io.PrintWriter;
20import java.util.Date;
21import java.util.Enumeration;
22import java.util.ResourceBundle;
23
24import javax.servlet.ServletException;
25import javax.servlet.http.HttpServlet;
26import javax.servlet.http.HttpServletRequest;
27import javax.servlet.http.HttpServletResponse;
28import javax.servlet.http.HttpSession;
Hongqing Liufd5ee812014-05-10 16:32:51 +080029
30import util.HTMLFilter;
31
32/**
33 * Example servlet showing request headers
34 *
35 * @author James Duncan Davidson <duncan@eng.sun.com>
36 */
37
38public class SessionExample extends HttpServlet {
39
刘洪青6266f992017-05-15 21:21:03 +080040 private static final long serialVersionUID = 1L;
41
42 private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");
43
44 @Override
Hongqing Liufd5ee812014-05-10 16:32:51 +080045 public void doGet(HttpServletRequest request,
46 HttpServletResponse response)
47 throws IOException, ServletException
48 {
49 response.setContentType("text/html");
50
51 PrintWriter out = response.getWriter();
52 out.println("<html>");
53 out.println("<head>");
54
刘洪青6266f992017-05-15 21:21:03 +080055 String title = RB.getString("sessions.title");
Hongqing Liufd5ee812014-05-10 16:32:51 +080056 out.println("<title>" + title + "</title>");
57 out.println("</head>");
58 out.println("<body bgcolor=\"white\">");
59
60 // img stuff not req'd for source code html showing
刘洪青6266f992017-05-15 21:21:03 +080061 // relative links everywhere!
Hongqing Liufd5ee812014-05-10 16:32:51 +080062
63 // XXX
64 // making these absolute till we work out the
刘洪青6266f992017-05-15 21:21:03 +080065 // addition of a PathInfo issue
66
Hongqing Liufd5ee812014-05-10 16:32:51 +080067 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);
刘洪青6266f992017-05-15 21:21:03 +080077 out.println(RB.getString("sessions.id") + " " + session.getId());
Hongqing Liufd5ee812014-05-10 16:32:51 +080078 out.println("<br>");
刘洪青6266f992017-05-15 21:21:03 +080079 out.println(RB.getString("sessions.created") + " ");
Hongqing Liufd5ee812014-05-10 16:32:51 +080080 out.println(new Date(session.getCreationTime()) + "<br>");
刘洪青6266f992017-05-15 21:21:03 +080081 out.println(RB.getString("sessions.lastaccessed") + " ");
Hongqing Liufd5ee812014-05-10 16:32:51 +080082 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>");
刘洪青6266f992017-05-15 21:21:03 +080091 out.println(RB.getString("sessions.data") + "<br>");
92 Enumeration<String> names = session.getAttributeNames();
Hongqing Liufd5ee812014-05-10 16:32:51 +080093 while (names.hasMoreElements()) {
刘洪青6266f992017-05-15 21:21:03 +080094 String name = names.nextElement();
Hongqing Liufd5ee812014-05-10 16:32:51 +080095 String value = session.getAttribute(name).toString();
刘洪青6266f992017-05-15 21:21:03 +080096 out.println(HTMLFilter.filter(name) + " = "
Hongqing Liufd5ee812014-05-10 16:32:51 +080097 + HTMLFilter.filter(value) + "<br>");
98 }
99
100 out.println("<P>");
101 out.print("<form action=\"");
刘洪青6266f992017-05-15 21:21:03 +0800102 out.print(response.encodeURL("SessionExample"));
Hongqing Liufd5ee812014-05-10 16:32:51 +0800103 out.print("\" ");
104 out.println("method=POST>");
刘洪青6266f992017-05-15 21:21:03 +0800105 out.println(RB.getString("sessions.dataname"));
Hongqing Liufd5ee812014-05-10 16:32:51 +0800106 out.println("<input type=text size=20 name=dataname>");
107 out.println("<br>");
刘洪青6266f992017-05-15 21:21:03 +0800108 out.println(RB.getString("sessions.datavalue"));
Hongqing Liufd5ee812014-05-10 16:32:51 +0800109 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=\"");
刘洪青6266f992017-05-15 21:21:03 +0800116 out.print(response.encodeURL("SessionExample"));
Hongqing Liufd5ee812014-05-10 16:32:51 +0800117 out.print("\" ");
118 out.println("method=GET>");
刘洪青6266f992017-05-15 21:21:03 +0800119 out.println(RB.getString("sessions.dataname"));
Hongqing Liufd5ee812014-05-10 16:32:51 +0800120 out.println("<input type=text size=20 name=dataname>");
121 out.println("<br>");
刘洪青6266f992017-05-15 21:21:03 +0800122 out.println(RB.getString("sessions.datavalue"));
Hongqing Liufd5ee812014-05-10 16:32:51 +0800123 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(HTMLFilter.filter(response.encodeURL("SessionExample?dataname=foo&datavalue=bar")));
130 out.println("\" >URL encoded </a>");
刘洪青6266f992017-05-15 21:21:03 +0800131
Hongqing Liufd5ee812014-05-10 16:32:51 +0800132 out.println("</body>");
133 out.println("</html>");
134 }
135
刘洪青6266f992017-05-15 21:21:03 +0800136 @Override
Hongqing Liufd5ee812014-05-10 16:32:51 +0800137 public void doPost(HttpServletRequest request,
138 HttpServletResponse response)
139 throws IOException, ServletException
140 {
141 doGet(request, response);
142 }
143
144}