use of javax.servlet.RequestDispatcher in project jetty.project by eclipse.
the class DispatchServlet method doGet.
/* ------------------------------------------------------------ */
@Override
public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException {
if (sreq.getParameter("wrap") != null) {
sreq = new HttpServletRequestWrapper(sreq);
sres = new HttpServletResponseWrapper(sres);
}
if (sreq.getParameter("session") != null)
sreq.getSession(true);
String prefix = sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath();
String info;
if (sreq.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) != null)
info = (String) sreq.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
else
info = sreq.getPathInfo();
if (info == null)
info = "NULL";
if (info.indexOf(sreq.getServletPath()) > 0) {
sres.sendError(403, "Nested " + sreq.getServletPath() + " forbidden.");
return;
}
if (info.indexOf(getServletName()) > 0) {
sres.sendError(403, "Nested " + getServletName() + " forbidden.");
return;
}
if (info.startsWith("/includeW/")) {
sres.setContentType("text/html");
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=include";
else
info += "&Dispatch=include";
PrintWriter pout = null;
pout = sres.getWriter();
pout.write("<H1>Include (writer): " + info + "</H1><HR>");
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch == null) {
pout = sres.getWriter();
pout.write("<H1>Null dispatcher</H1>");
} else
dispatch.include(sreq, sres);
pout.write("<HR><H1>-- Included (writer)</H1>");
} else if (info.startsWith("/includeS/")) {
sres.setContentType("text/html");
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=include";
else
info += "&Dispatch=include";
OutputStream out = null;
out = sres.getOutputStream();
out.write(("<H1>Include (outputstream): " + info + "</H1><HR>").getBytes());
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch == null) {
out = sres.getOutputStream();
out.write("<H1>Null dispatcher</H1>".getBytes());
} else
dispatch.include(sreq, sres);
out.write("<HR><H1>-- Included (outputstream)</H1>".getBytes());
} else if (info.startsWith("/forward/")) {
info = info.substring(8);
if (info.indexOf('?') < 0)
info += "?Dispatch=forward";
else
info += "&Dispatch=forward";
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch != null) {
ServletOutputStream out = sres.getOutputStream();
out.print("Can't see this");
dispatch.forward(sreq, sres);
try {
// should be closed
out.println("IOException");
// should not get here
throw new IllegalStateException();
} catch (IOException e) {
// getServletContext().log("ignore",e);
}
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No dispatcher for: " + info + "</H1><HR>");
pout.flush();
}
} else if (info.startsWith("/forwardC/")) {
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=forward";
else
info += "&Dispatch=forward";
String cpath = info.substring(0, info.indexOf('/', 1));
info = info.substring(cpath.length());
ServletContext context = getServletContext().getContext(cpath);
RequestDispatcher dispatch = context.getRequestDispatcher(info);
if (dispatch != null) {
dispatch.forward(sreq, sres);
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No dispatcher for: " + cpath + "/" + info + "</H1><HR>");
pout.flush();
}
} else if (info.startsWith("/includeN/")) {
sres.setContentType("text/html");
info = info.substring(10);
if (info.indexOf("/") >= 0)
info = info.substring(0, info.indexOf("/"));
PrintWriter pout;
if (info.startsWith("/null"))
info = info.substring(5);
else {
pout = sres.getWriter();
pout.write("<H1>Include named: " + info + "</H1><HR>");
}
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info);
if (dispatch != null)
dispatch.include(sreq, sres);
else {
pout = sres.getWriter();
pout.write("<H1>No servlet named: " + info + "</H1>");
}
pout = sres.getWriter();
pout.write("<HR><H1>Included ");
} else if (info.startsWith("/forwardN/")) {
info = info.substring(10);
if (info.indexOf("/") >= 0)
info = info.substring(0, info.indexOf("/"));
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info);
if (dispatch != null)
dispatch.forward(sreq, sres);
else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No servlet named: " + info + "</H1>");
pout.flush();
}
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>Dispatch URL must be of the form: </H1>" + "<PRE>" + prefix + "/includeW/path\n" + prefix + "/includeS/path\n" + prefix + "/forward/path\n" + prefix + "/includeN/name\n" + prefix + "/forwardC/_context/path\n</PRE>");
}
}
use of javax.servlet.RequestDispatcher in project jetty.project by eclipse.
the class ConcatServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getQueryString();
if (query == null) {
response.sendError(HttpServletResponse.SC_NO_CONTENT);
return;
}
List<RequestDispatcher> dispatchers = new ArrayList<>();
String[] parts = query.split("\\&");
String type = null;
for (String part : parts) {
String path = URIUtil.canonicalPath(URIUtil.decodePath(part));
if (path == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// Verify that the path is not protected.
if (startsWith(path, "/WEB-INF/") || startsWith(path, "/META-INF/")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String t = getServletContext().getMimeType(path);
if (t != null) {
if (type == null) {
type = t;
} else if (!type.equals(t)) {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
return;
}
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
if (dispatcher != null)
dispatchers.add(dispatcher);
}
if (type != null)
response.setContentType(type);
for (RequestDispatcher dispatcher : dispatchers) dispatcher.include(request, response);
}
use of javax.servlet.RequestDispatcher in project jersey by jersey.
the class Include method doTag.
public void doTag() throws JspException, IOException {
final JspContext jspContext = getJspContext();
final Class<?> resolvingClass = (Class<?>) jspContext.getAttribute(RequestDispatcherWrapper.RESOLVING_CLASS_ATTRIBUTE_NAME, PageContext.REQUEST_SCOPE);
final String basePath = (String) jspContext.getAttribute(RequestDispatcherWrapper.BASE_PATH_ATTRIBUTE_NAME, PageContext.REQUEST_SCOPE);
final ServletConfig servletConfig = (ServletConfig) getPageObject(PageContext.CONFIG);
final ServletContext servletContext = servletConfig.getServletContext();
for (Class<?> clazz = resolvingClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
final String template = basePath + TemplateHelper.getAbsolutePath(clazz, page, '/');
if (servletContext.getResource(template) != null) {
// Tomcat returns a RequestDispatcher even if the JSP file doesn't exist so check if the resource exists first.
final RequestDispatcher dispatcher = servletContext.getRequestDispatcher(template);
if (dispatcher != null) {
try {
final HttpServletRequest request = (HttpServletRequest) getPageObject(PageContext.REQUEST);
final HttpServletResponse response = (HttpServletResponse) getPageObject(PageContext.RESPONSE);
dispatcher.include(request, new Wrapper(response, new PrintWriter(jspContext.getOut())));
} catch (ServletException e) {
throw new JspException(e);
}
return;
}
}
}
throw new JspException(LocalizationMessages.UNABLE_TO_FIND_PAGE_FOR_RESOLVING_CLASS(page, resolvingClass));
}
use of javax.servlet.RequestDispatcher in project hadoop by apache.
the class DefaultWrapperServlet method doGet.
@Private
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher rd = getServletContext().getNamedDispatcher("default");
HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
public String getServletPath() {
return "";
}
};
rd.forward(wrapped, resp);
}
use of javax.servlet.RequestDispatcher in project sonarqube by SonarSource.
the class RequestProcessor method doInclude.
/**
* <p>Do an include of specified URI using a <code>RequestDispatcher</code>.
* This method is used by all internal method needing to do an
* include.</p>
*
* @param uri Context-relative URI to include
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @since Struts 1.1
*/
protected void doInclude(String uri, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.include(request, response);
}
Aggregations