test dispatch webapp

This commit is contained in:
Greg Wilkins 2014-05-12 12:15:58 +02:00
parent 2ba7334868
commit 98d41be310
7 changed files with 222 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-webapps-parent</artifactId>
<version>9.2.0-SNAPSHOT</version>
</parent>
<name>Jetty Tests :: Webapps :: Dispatch Webapp</name>
<artifactId>test-dispatch-webapp</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<useTestClasspath>true</useTestClasspath>
<webAppConfig>
<war>src/main/webapp</war>
<descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
<contextPath>/test-dispatch</contextPath>
<containerIncludeJarPattern>.*/javax.servlet-[^/]*\.jar$</containerIncludeJarPattern>
<configurationDiscovered>true</configurationDiscovered>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,117 @@
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package com.acme;
import static javax.servlet.RequestDispatcher.FORWARD_PATH_INFO;
import static javax.servlet.RequestDispatcher.FORWARD_QUERY_STRING;
import static javax.servlet.RequestDispatcher.FORWARD_SERVLET_PATH;
import static javax.servlet.RequestDispatcher.INCLUDE_PATH_INFO;
import static javax.servlet.RequestDispatcher.INCLUDE_QUERY_STRING;
import static javax.servlet.RequestDispatcher.INCLUDE_REQUEST_URI;
import static javax.servlet.RequestDispatcher.INCLUDE_SERVLET_PATH;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DispatchServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Integer depth = (Integer)request.getAttribute("depth");
if (depth==null)
depth=1;
else
depth=depth+1;
request.setAttribute("depth", depth);
String path=request.getServletPath();
String info=request.getPathInfo();
String query=request.getQueryString();
boolean include=request.getAttribute(INCLUDE_REQUEST_URI)!=null;
String tpath = include?(String)request.getAttribute(INCLUDE_SERVLET_PATH):path;
String tinfo = include?(String)request.getAttribute(INCLUDE_PATH_INFO):info;
if ("/forward".equals(tpath))
{
getServletContext().getRequestDispatcher(tinfo+"?depth="+depth+"&p"+depth+"="+"ABCDEFGHI".charAt(depth)).forward(request, response);
}
else if ("/include".equals(tpath))
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Dispatch Depth="+depth+"</h1><pre>");
out.printf(" %30s%30s%30s%n","REQUEST","FORWARD","INCLUDE");
out.printf("servletPath:%30s%30s%30s%n",path,request.getAttribute(FORWARD_SERVLET_PATH),request.getAttribute(INCLUDE_SERVLET_PATH));
out.printf(" pathInfo:%30s%30s%30s%n",info,request.getAttribute(FORWARD_PATH_INFO),request.getAttribute(INCLUDE_PATH_INFO));
out.printf(" query:%30s%30s%30s%n",query,request.getAttribute(FORWARD_QUERY_STRING),request.getAttribute(INCLUDE_QUERY_STRING));
out.println();
printParameters(out, request.getParameterMap());
out.println("</pre>");
out.println("<hr/>");
getServletContext().getRequestDispatcher(tinfo+"?depth="+depth+"&p"+depth+"="+"BCDEFGHI".charAt(depth)).include(request, response);
out.println("<hr/>");
out.println("<h1>Dispatch Depth="+depth+"</h1><pre>");
out.printf(" %30s%30s%30s%n","REQUEST","FORWARD","INCLUDE");
out.printf("servletPath:%30s%30s%30s%n",path,request.getAttribute(FORWARD_SERVLET_PATH),request.getAttribute(INCLUDE_SERVLET_PATH));
out.printf(" pathInfo:%30s%30s%30s%n",info,request.getAttribute(FORWARD_PATH_INFO),request.getAttribute(INCLUDE_PATH_INFO));
out.printf(" query:%30s%30s%30s%n",query,request.getAttribute(FORWARD_QUERY_STRING),request.getAttribute(INCLUDE_QUERY_STRING));
out.println();
printParameters(out, request.getParameterMap());
out.println("</pre>");
}
else
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Dispatch Depth="+depth+"</h1><pre>");
out.printf(" %30s%30s%30s%n","REQUEST","FORWARD","INCLUDE");
out.printf("servletPath:%30s%30s%30s%n",path,request.getAttribute(FORWARD_SERVLET_PATH),request.getAttribute(INCLUDE_SERVLET_PATH));
out.printf(" pathInfo:%30s%30s%30s%n",info,request.getAttribute(FORWARD_PATH_INFO),request.getAttribute(INCLUDE_PATH_INFO));
out.printf(" query:%30s%30s%30s%n",query,request.getAttribute(FORWARD_QUERY_STRING),request.getAttribute(INCLUDE_QUERY_STRING));
out.println();
printParameters(out, request.getParameterMap());
out.println("</pre>");
}
}
private static void printParameters(PrintWriter out, Map<String,String[]> params)
{
List<String> names = new ArrayList<>(params.keySet());
Collections.sort(names);
for (String name: names)
out.printf("%10s : %s%n", name,Arrays.asList(params.get(name)));
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false"
version="3.1">
<display-name>Test Dispatch WebApp</display-name>
<servlet>
<servlet-name>Dispatch</servlet-name>
<servlet-class>com.acme.DispatchServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatch</servlet-name>
<url-pattern>/forward/*</url-pattern>
<url-pattern>/include/*</url-pattern>
<url-pattern>/info/*</url-pattern>
</servlet-mapping>
</web-app>

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -0,0 +1,28 @@
<HTML>
<HEAD>
<TITLE>Test Dispatch WebApp</TITLE>
<META http-equiv="Pragma" content="no-cache">
<META http-equiv="Cache-Control" content="no-cache,no-store">
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</HEAD>
<BODY >
<A HREF="http://www.eclipse.org/jetty"><IMG SRC="images/jetty_banner.gif"></A>
<h1>Request Dispatching Tests</h1>
<p><ul>
<li><a href="forward/info?depth=0&p0=A">forward/info?depth=0&p0=A</a></li>
<li><a href="include/info?depth=0&p0=A">include/info?depth=0&p0=A</a></li>
<li><a href="forward/forward/info?depth=0&p0=A">forward/forward/info?depth=0&p0=A</a></li>
<li><a href="forward/include/info?depth=0&p0=A">forward/include/info?depth=0&p0=A</a></li>
<li><a href="include/forward/info?depth=0&p0=A">include/forward/info?depth=0&p0=A</a></li>
<li><a href="include/include/info?depth=0&p0=A">include/include/info?depth=0&p0=A</a></li>
</ul>
</p>
<center>
<hr/>
<a href="http://www.eclipse.org/jetty"><img style="border:0" src="images/small_powered_by.gif"/></a>
</center>
</BODY>
</HTML>

View File

@ -0,0 +1,7 @@
body {color: #2E2E2E; font-family:sans-serif; font-size:90%;}
h1 {font-variant: small-caps; font-size:130%; letter-spacing: 0.1em;}
h2 {font-variant: small-caps; font-size:100%; letter-spacing: 0.1em; margin-top:2em;}
h3 {font-size:100%; letter-spacing: 0.1em;}
span.pass { color: green; }
span.fail { color:red; }