| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// |
| 5 |
// This program and the accompanying materials are made available under the |
| 6 |
// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 |
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 |
// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 |
// |
| 10 |
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 |
// ======================================================================== |
| 12 |
// |
| 13 |
|
| 14 |
package org.example; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.io.PrintWriter; |
| 18 |
import java.util.concurrent.TimeUnit; |
| 19 |
|
| 20 |
import jakarta.servlet.RequestDispatcher; |
| 21 |
import jakarta.servlet.ServletException; |
| 22 |
import jakarta.servlet.http.Cookie; |
| 23 |
import jakarta.servlet.http.HttpServlet; |
| 24 |
import jakarta.servlet.http.HttpServletRequest; |
| 25 |
import jakarta.servlet.http.HttpServletResponse; |
| 26 |
|
| 27 |
/** |
| 28 |
* Test Servlet Cookies. |
| 29 |
*/ |
| 30 |
@SuppressWarnings("serial") |
| 31 |
public class CookieDump extends HttpServlet |
| 32 |
{ |
| 33 |
int redirectCount = 0; |
| 34 |
|
| 35 |
protected void handleForm(HttpServletRequest request, |
| 36 |
HttpServletResponse response) |
| 37 |
{ |
| 38 |
String name = request.getParameter("Name"); |
| 39 |
String value = request.getParameter("Value"); |
| 40 |
String age = request.getParameter("Age"); |
| 41 |
|
| 42 |
if (name != null && !name.isEmpty()) |
| 43 |
{ |
| 44 |
Cookie cookie = new Cookie(name, value); |
| 45 |
if (age != null && !age.isEmpty()) |
| 46 |
cookie.setMaxAge(Integer.parseInt(age)); |
| 47 |
response.addCookie(cookie); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void doPost(HttpServletRequest request, |
| 53 |
HttpServletResponse response) |
| 54 |
throws ServletException, IOException |
| 55 |
{ |
| 56 |
handleForm(request, response); |
| 57 |
String nextUrl = getURI(request) + "?R=" + redirectCount++; |
| 58 |
String encodedUrl = response.encodeRedirectURL(nextUrl); |
| 59 |
response.sendRedirect(encodedUrl); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void doGet(HttpServletRequest request, |
| 64 |
HttpServletResponse response) |
| 65 |
throws ServletException, IOException |
| 66 |
{ |
| 67 |
handleForm(request, response); |
| 68 |
|
| 69 |
response.setContentType("text/html"); |
| 70 |
|
| 71 |
PrintWriter out = response.getWriter(); |
| 72 |
out.println("<h1>Cookie Dump Servlet:</h1>"); |
| 73 |
|
| 74 |
Cookie[] cookies = request.getCookies(); |
| 75 |
|
| 76 |
for (int i = 0; cookies != null && i < cookies.length; i++) |
| 77 |
{ |
| 78 |
out.println("<b>" + deScript(cookies[i].getName()) + "</b>=" + deScript(cookies[i].getValue()) + "<br/>"); |
| 79 |
} |
| 80 |
|
| 81 |
out.println("<form action=\"" + response.encodeURL(getURI(request)) + "\" method=\"post\">"); |
| 82 |
|
| 83 |
out.println("<b>Name:</b><input type=\"text\" name=\"Name\" value=\"name\"/><br/>"); |
| 84 |
out.println("<b>Value:</b><input type=\"text\" name=\"Value\" value=\"value\"/><br/>"); |
| 85 |
out.println("<b>Max-Age:</b><input type=\"text\" name=\"Age\" value=\"60\"/><br/>"); |
| 86 |
out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>"); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public String getServletInfo() |
| 91 |
{ |
| 92 |
return "Session Dump Servlet"; |
| 93 |
} |
| 94 |
|
| 95 |
private String getURI(HttpServletRequest request) |
| 96 |
{ |
| 97 |
String uri = (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); |
| 98 |
if (uri == null) |
| 99 |
uri = request.getRequestURI(); |
| 100 |
return uri; |
| 101 |
} |
| 102 |
|
| 103 |
protected String deScript(String string) |
| 104 |
{ |
| 105 |
if (string == null) |
| 106 |
return null; |
| 107 |
string = string.replace("&", "&"); |
| 108 |
string = string.replace("<", "<"); |
| 109 |
string = string.replace(">", ">"); |
| 110 |
return string; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void destroy() |
| 115 |
{ |
| 116 |
// For testing --stop with STOP.WAIT handling of the jetty-start behavior. |
| 117 |
if (Boolean.getBoolean("test.slow.destroy")) |
| 118 |
{ |
| 119 |
try |
| 120 |
{ |
| 121 |
TimeUnit.SECONDS.sleep(10); |
| 122 |
} |
| 123 |
catch (InterruptedException e) |
| 124 |
{ |
| 125 |
// ignore |
| 126 |
} |
| 127 |
} |
| 128 |
super.destroy(); |
| 129 |
} |
| 130 |
} |