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