| 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.OutputStream; |
| 18 |
import java.io.PrintWriter; |
| 19 |
|
| 20 |
import jakarta.servlet.RequestDispatcher; |
| 21 |
import jakarta.servlet.ServletContext; |
| 22 |
import jakarta.servlet.ServletException; |
| 23 |
import jakarta.servlet.ServletOutputStream; |
| 24 |
import jakarta.servlet.http.HttpServlet; |
| 25 |
import jakarta.servlet.http.HttpServletRequest; |
| 26 |
import jakarta.servlet.http.HttpServletRequestWrapper; |
| 27 |
import jakarta.servlet.http.HttpServletResponse; |
| 28 |
import jakarta.servlet.http.HttpServletResponseWrapper; |
| 29 |
|
| 30 |
/** |
| 31 |
* Test Servlet RequestDispatcher. |
| 32 |
*/ |
| 33 |
@SuppressWarnings("serial") |
| 34 |
public class DispatchServlet extends HttpServlet |
| 35 |
{ |
| 36 |
|
| 37 |
String pageType; |
| 38 |
|
| 39 |
@Override |
| 40 |
public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException |
| 41 |
{ |
| 42 |
doGet(sreq, sres); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException |
| 47 |
{ |
| 48 |
if (sreq.getParameter("wrap") != null) |
| 49 |
{ |
| 50 |
sreq = new HttpServletRequestWrapper(sreq); |
| 51 |
sres = new HttpServletResponseWrapper(sres); |
| 52 |
} |
| 53 |
|
| 54 |
if (sreq.getParameter("session") != null) |
| 55 |
sreq.getSession(true); |
| 56 |
|
| 57 |
String prefix = |
| 58 |
sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath(); |
| 59 |
|
| 60 |
String info; |
| 61 |
|
| 62 |
if (sreq.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) != null) |
| 63 |
info = (String)sreq.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO); |
| 64 |
else |
| 65 |
info = sreq.getPathInfo(); |
| 66 |
|
| 67 |
if (info == null) |
| 68 |
info = "NULL"; |
| 69 |
|
| 70 |
if (info.indexOf(sreq.getServletPath()) > 0) |
| 71 |
{ |
| 72 |
sres.sendError(403, "Nested " + sreq.getServletPath() + " forbidden."); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if (info.indexOf(getServletName()) > 0) |
| 77 |
{ |
| 78 |
sres.sendError(403, "Nested " + getServletName() + " forbidden."); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if (info.startsWith("/includeW/")) |
| 83 |
{ |
| 84 |
sres.setContentType("text/html"); |
| 85 |
info = info.substring(9); |
| 86 |
if (info.indexOf('?') < 0) |
| 87 |
info += "?Dispatch=include"; |
| 88 |
else |
| 89 |
info += "&Dispatch=include"; |
| 90 |
|
| 91 |
PrintWriter pout = null; |
| 92 |
pout = sres.getWriter(); |
| 93 |
pout.write("<h1>Include (writer): " + info + "</h1><hR>"); |
| 94 |
|
| 95 |
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info); |
| 96 |
if (dispatch == null) |
| 97 |
{ |
| 98 |
pout = sres.getWriter(); |
| 99 |
pout.write("<h1>Null dispatcher</h1>"); |
| 100 |
} |
| 101 |
else |
| 102 |
dispatch.include(sreq, sres); |
| 103 |
|
| 104 |
pout.write("<hR><h1>-- Included (writer)</h1>"); |
| 105 |
} |
| 106 |
else if (info.startsWith("/includeS/")) |
| 107 |
{ |
| 108 |
sres.setContentType("text/html"); |
| 109 |
info = info.substring(9); |
| 110 |
if (info.indexOf('?') < 0) |
| 111 |
info += "?Dispatch=include"; |
| 112 |
else |
| 113 |
info += "&Dispatch=include"; |
| 114 |
|
| 115 |
OutputStream out = null; |
| 116 |
out = sres.getOutputStream(); |
| 117 |
out.write(("<h1>Include (outputstream): " + info + "</h1><hR>").getBytes()); |
| 118 |
|
| 119 |
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info); |
| 120 |
if (dispatch == null) |
| 121 |
{ |
| 122 |
out = sres.getOutputStream(); |
| 123 |
out.write("<h1>Null dispatcher</h1>".getBytes()); |
| 124 |
} |
| 125 |
else |
| 126 |
dispatch.include(sreq, sres); |
| 127 |
|
| 128 |
out.write("<hR><h1>-- Included (outputstream)</h1>".getBytes()); |
| 129 |
} |
| 130 |
else if (info.startsWith("/forward/")) |
| 131 |
{ |
| 132 |
info = info.substring(8); |
| 133 |
if (info.indexOf('?') < 0) |
| 134 |
info += "?Dispatch=forward"; |
| 135 |
else |
| 136 |
info += "&Dispatch=forward"; |
| 137 |
|
| 138 |
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info); |
| 139 |
if (dispatch != null) |
| 140 |
{ |
| 141 |
ServletOutputStream out = sres.getOutputStream(); |
| 142 |
out.print("Can't see this"); |
| 143 |
dispatch.forward(sreq, sres); |
| 144 |
try |
| 145 |
{ |
| 146 |
// should be closed |
| 147 |
out.println("IOException"); |
| 148 |
// should not get here |
| 149 |
throw new IllegalStateException(); |
| 150 |
} |
| 151 |
catch (IOException e) |
| 152 |
{ |
| 153 |
// getServletContext().log("ignore",e); |
| 154 |
} |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
sres.setContentType("text/html"); |
| 159 |
PrintWriter pout = sres.getWriter(); |
| 160 |
pout.write("<h1>No dispatcher for: " + info + "</h1><hR>"); |
| 161 |
pout.flush(); |
| 162 |
} |
| 163 |
} |
| 164 |
else if (info.startsWith("/forwardC/")) |
| 165 |
{ |
| 166 |
info = info.substring(9); |
| 167 |
if (info.indexOf('?') < 0) |
| 168 |
info += "?Dispatch=forward"; |
| 169 |
else |
| 170 |
info += "&Dispatch=forward"; |
| 171 |
|
| 172 |
String cpath = info.substring(0, info.indexOf('/', 1)); |
| 173 |
info = info.substring(cpath.length()); |
| 174 |
|
| 175 |
ServletContext context = getServletContext().getContext(cpath); |
| 176 |
RequestDispatcher dispatch = context.getRequestDispatcher(info); |
| 177 |
|
| 178 |
if (dispatch != null) |
| 179 |
{ |
| 180 |
dispatch.forward(sreq, sres); |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
sres.setContentType("text/html"); |
| 185 |
PrintWriter pout = sres.getWriter(); |
| 186 |
pout.write("<h1>No dispatcher for: " + cpath + "/" + info + "</h1><hR>"); |
| 187 |
pout.flush(); |
| 188 |
} |
| 189 |
} |
| 190 |
else if (info.startsWith("/includeN/")) |
| 191 |
{ |
| 192 |
sres.setContentType("text/html"); |
| 193 |
info = info.substring(10); |
| 194 |
if (info.indexOf("/") >= 0) |
| 195 |
info = info.substring(0, info.indexOf("/")); |
| 196 |
|
| 197 |
PrintWriter pout; |
| 198 |
if (info.startsWith("/null")) |
| 199 |
info = info.substring(5); |
| 200 |
else |
| 201 |
{ |
| 202 |
pout = sres.getWriter(); |
| 203 |
pout.write("<h1>Include named: " + info + "</h1><hR>"); |
| 204 |
} |
| 205 |
|
| 206 |
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info); |
| 207 |
if (dispatch != null) |
| 208 |
dispatch.include(sreq, sres); |
| 209 |
else |
| 210 |
{ |
| 211 |
pout = sres.getWriter(); |
| 212 |
pout.write("<h1>No servlet named: " + info + "</h1>"); |
| 213 |
} |
| 214 |
|
| 215 |
pout = sres.getWriter(); |
| 216 |
pout.write("<hR><h1>Included "); |
| 217 |
} |
| 218 |
else if (info.startsWith("/forwardN/")) |
| 219 |
{ |
| 220 |
info = info.substring(10); |
| 221 |
if (info.indexOf("/") >= 0) |
| 222 |
info = info.substring(0, info.indexOf("/")); |
| 223 |
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info); |
| 224 |
if (dispatch != null) |
| 225 |
dispatch.forward(sreq, sres); |
| 226 |
else |
| 227 |
{ |
| 228 |
sres.setContentType("text/html"); |
| 229 |
PrintWriter pout = sres.getWriter(); |
| 230 |
pout.write("<h1>No servlet named: " + info + "</h1>"); |
| 231 |
pout.flush(); |
| 232 |
} |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
sres.setContentType("text/html"); |
| 237 |
PrintWriter pout = sres.getWriter(); |
| 238 |
pout.write( |
| 239 |
"<h1>Dispatch URL must be of the form: </h1>" + |
| 240 |
"<PRE>" + |
| 241 |
prefix + "/includeW/path\n" + |
| 242 |
prefix + "/includeS/path\n" + |
| 243 |
prefix + "/forward/path\n" + |
| 244 |
prefix + "/includeN/name\n" + |
| 245 |
prefix + "/forwardC/_context/path\n</PRE>"); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public String getServletInfo() |
| 251 |
{ |
| 252 |
return "Include Servlet"; |
| 253 |
} |
| 254 |
} |