Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 1 | |
| 2 | /* |
||
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
||
| 4 | * contributor license agreements. See the NOTICE file distributed with |
||
| 5 | * this work for additional information regarding copyright ownership. |
||
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
||
| 7 | * (the "License"); you may not use this file except in compliance with |
||
| 8 | * the License. You may obtain a copy of the License at |
||
| 9 | * |
||
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 11 | * |
||
| 12 | * Unless required by applicable law or agreed to in writing, software |
||
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
| 15 | * See the License for the specific language governing permissions and |
||
| 16 | * limitations under the License. |
||
| 17 | */ |
||
| 18 | import java.io.IOException; |
||
| 19 | import java.io.PrintWriter; |
||
| 20 | import java.net.URLEncoder; |
||
| 21 | import java.util.Date; |
||
| 22 | import java.util.Enumeration; |
||
| 23 | import java.util.ResourceBundle; |
||
| 24 | |||
| 25 | import javax.servlet.ServletException; |
||
| 26 | import javax.servlet.http.HttpServlet; |
||
| 27 | import javax.servlet.http.HttpServletRequest; |
||
| 28 | import javax.servlet.http.HttpServletResponse; |
||
| 29 | import javax.servlet.http.HttpSession; |
||
| 30 | |||
| 31 | import util.HTMLFilter; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Example servlet showing request headers |
||
| 35 | */ |
||
| 36 | public class SessionExample extends HttpServlet { |
||
| 37 | |||
| 38 | private static final long serialVersionUID = 1L; |
||
| 39 | |||
| 40 | private static final int SESSION_ATTRIBUTE_COUNT_LIMIT = 10; |
||
| 41 | |||
| 42 | |||
| 43 | @Override |
||
| 44 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||
| 45 | ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", request.getLocale()); |
||
| 46 | |||
| 47 | response.setContentType("text/html"); |
||
| 48 | response.setCharacterEncoding("UTF-8"); |
||
| 49 | |||
| 50 | PrintWriter out = response.getWriter(); |
||
| 51 | out.println("<!DOCTYPE html><html>"); |
||
| 52 | out.println("<head>"); |
||
| 53 | out.println("<meta charset=\"UTF-8\" />"); |
||
| 54 | |||
| 55 | |||
| 56 | String title = rb.getString("sessions.title"); |
||
| 57 | out.println("<title>" + title + "</title>"); |
||
| 58 | out.println("</head>"); |
||
| 59 | out.println("<body bgcolor=\"white\">"); |
||
| 60 | |||
| 61 | out.println("<a href=\"../sessions.html\">"); |
||
| 62 | out.println( |
||
| 63 | "<img src=\"../images/code.gif\" height=24 " + "width=24 align=right border=0 alt=\"view code\"></a>"); |
||
| 64 | out.println("<a href=\"../index.html\">"); |
||
| 65 | out.println( |
||
| 66 | "<img src=\"../images/return.gif\" height=24 " + "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 | // Count the existing attributes |
||
| 79 | int sessionAttributeCount = 0; |
||
| 80 | Enumeration<String> names = session.getAttributeNames(); |
||
| 81 | while (names.hasMoreElements()) { |
||
| 82 | names.nextElement(); |
||
| 83 | sessionAttributeCount++; |
||
| 84 | } |
||
| 85 | |||
| 86 | String dataName = request.getParameter("dataname"); |
||
| 87 | String dataValue = request.getParameter("datavalue"); |
||
| 88 | if (dataName != null) { |
||
| 89 | if (dataValue == null) { |
||
| 90 | session.removeAttribute(dataName); |
||
| 91 | sessionAttributeCount--; |
||
| 92 | } else if (sessionAttributeCount < SESSION_ATTRIBUTE_COUNT_LIMIT) { |
||
| 93 | session.setAttribute(dataName, dataValue); |
||
| 94 | sessionAttributeCount++; |
||
| 95 | } else { |
||
| 96 | out.print("<p> Session attribute ["); |
||
| 97 | out.print(HTMLFilter.filter(dataName)); |
||
| 98 | out.print("] not added as there are already "+ SESSION_ATTRIBUTE_COUNT_LIMIT + " attributes in the "); |
||
| 99 | out.println("session. Delete an attribute before adding another."); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | out.println("<p>"); |
||
| 104 | out.println(rb.getString("sessions.data") + "<br>"); |
||
| 105 | names = session.getAttributeNames(); |
||
| 106 | while (names.hasMoreElements()) { |
||
| 107 | String name = names.nextElement(); |
||
| 108 | String value = session.getAttribute(name).toString(); |
||
| 109 | out.println(HTMLFilter.filter(name) + " = " + HTMLFilter.filter(value)); |
||
| 110 | out.print("<a href=\""); |
||
| 111 | out.print(HTMLFilter.filter( |
||
| 112 | response.encodeURL("SessionExample?dataname=" + URLEncoder.encode(name, "UTF-8")))); |
||
| 113 | out.println("\" >delete</a>"); |
||
| 114 | out.println("<br>"); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (sessionAttributeCount < SESSION_ATTRIBUTE_COUNT_LIMIT) { |
||
| 118 | out.println("<p>"); |
||
| 119 | out.print("<form action=\""); |
||
| 120 | out.print(response.encodeURL("SessionExample")); |
||
| 121 | out.print("\" "); |
||
| 122 | out.println("method=POST>"); |
||
| 123 | out.println(rb.getString("sessions.dataname")); |
||
| 124 | out.println("<input type=text size=20 name=dataname>"); |
||
| 125 | out.println("<br>"); |
||
| 126 | out.println(rb.getString("sessions.datavalue")); |
||
| 127 | out.println("<input type=text size=20 name=datavalue>"); |
||
| 128 | out.println("<br>"); |
||
| 129 | out.println("<input type=submit>"); |
||
| 130 | out.println("</form>"); |
||
| 131 | |||
| 132 | out.println("<p>GET based form:<br>"); |
||
| 133 | out.print("<form action=\""); |
||
| 134 | out.print(response.encodeURL("SessionExample")); |
||
| 135 | out.print("\" "); |
||
| 136 | out.println("method=GET>"); |
||
| 137 | out.println(rb.getString("sessions.dataname")); |
||
| 138 | out.println("<input type=text size=20 name=dataname>"); |
||
| 139 | out.println("<br>"); |
||
| 140 | out.println(rb.getString("sessions.datavalue")); |
||
| 141 | out.println("<input type=text size=20 name=datavalue>"); |
||
| 142 | out.println("<br>"); |
||
| 143 | out.println("<input type=submit>"); |
||
| 144 | out.println("</form>"); |
||
| 145 | |||
| 146 | out.print("<p><a href=\""); |
||
| 147 | out.print(HTMLFilter.filter(response.encodeURL("SessionExample?dataname=exampleName&datavalue=exampleValue"))); |
||
| 148 | out.println("\" >URL encoded </a>"); |
||
| 149 | } else { |
||
| 150 | out.print("<p>You may not add more than " + SESSION_ATTRIBUTE_COUNT_LIMIT + " attributes to this session."); |
||
| 151 | } |
||
| 152 | |||
| 153 | out.println("</body>"); |
||
| 154 | out.println("</html>"); |
||
| 155 | } |
||
| 156 | |||
| 157 | @Override |
||
| 158 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||
| 159 | doGet(request, response); |
||
| 160 | } |
||
| 161 | |||
| 162 | } |