diff options
author | Max Romanov <max.romanov@gmail.com> | 2019-02-28 18:02:42 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@gmail.com> | 2019-02-28 18:02:42 +0300 |
commit | 5bfdebb9e4161a689113d73775498949a09d7fb5 (patch) | |
tree | fcb69169e3da983db0eb1f48d2dccc2ac2ff867b /test/java/include | |
parent | ec7319d32c9c41597a99a9422ff324c97a92bb21 (diff) | |
download | unit-5bfdebb9e4161a689113d73775498949a09d7fb5.tar.gz unit-5bfdebb9e4161a689113d73775498949a09d7fb5.tar.bz2 |
Introducing Java Servlet Container beta.
Diffstat (limited to '')
-rw-r--r-- | test/java/include/app.java | 136 | ||||
-rw-r--r-- | test/java/include/index.html | 1 | ||||
-rw-r--r-- | test/java/include/web.xml | 37 |
3 files changed, 174 insertions, 0 deletions
diff --git a/test/java/include/app.java b/test/java/include/app.java new file mode 100644 index 00000000..d7e36fc6 --- /dev/null +++ b/test/java/include/app.java @@ -0,0 +1,136 @@ + +import java.io.IOException; +import java.io.PrintWriter; + +import java.util.Map; + +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; + +public class app extends HttpServlet +{ + private String id; + + private class RequestWrapper extends HttpServletRequestWrapper + { + public RequestWrapper(HttpServletRequest r) + { + super(r); + } + } + + private class ResponseWrapper extends HttpServletResponseWrapper + { + public ResponseWrapper(HttpServletResponse r) + { + super(r); + } + } + + @Override + public void init(ServletConfig sc) + throws ServletException + { + id = sc.getInitParameter("id"); + } + + private RequestDispatcher getRequestDispatcher(HttpServletRequest request, String str) + { + String disp = request.getParameter("disp"); + + if (disp != null && disp.equals("ctx")) { + return request.getServletContext().getRequestDispatcher(str); + } + + if (disp != null && disp.equals("name")) { + return request.getServletContext().getNamedDispatcher(str); + } + + if (disp == null || disp.equals("req")) { + return request.getRequestDispatcher(str); + } + + return null; + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException + { + String dtype = "" + request.getDispatcherType(); + + response.addHeader("X-" + dtype + "-Id", id); + response.addHeader("X-" + dtype + "-Request-URI", "" + request.getRequestURI()); + response.addHeader("X-" + dtype + "-Servlet-Path", "" + request.getServletPath()); + response.addHeader("X-" + dtype + "-Path-Info", "" + request.getPathInfo()); + response.addHeader("X-" + dtype + "-Query-String", "" + request.getQueryString()); + response.addHeader("X-" + dtype + "-Dispatcher-Type", "" + request.getDispatcherType()); + + response.setContentType("text/plain; charset=utf-8"); + + PrintWriter out = response.getWriter(); + + if (id.equals("inc")) { + String uri = request.getParameter("uri"); + + if (uri != null && request.getDispatcherType() != DispatcherType.INCLUDE) { + response.addHeader("X-Include", "" + uri); + + out.println("Before include."); + + RequestDispatcher d = getRequestDispatcher(request, uri); + + if (d == null) { + out.println("Dispatcher is null"); + return; + } + + try { + d.include(new RequestWrapper(request), new ResponseWrapper(response)); + } catch(Exception e) { + response.addHeader("X-Exception", "" + e); + out.println("Exception: " + e); + } + + response.addHeader("X-After-Include", "you-should-see-this"); + + out.println("After include."); + + return; + } + } + + if (id.equals("data")) { + out.println("app.doGet(): #" + this + ", " + id); + out.println("RequestURI: " + request.getRequestURI()); + out.println("ServletPath: " + request.getServletPath()); + out.println("PathInfo: " + request.getPathInfo()); + out.println("DispType: " + request.getDispatcherType()); + out.println("QueryString: " + request.getQueryString()); + + Map<String, String[]> pmap = request.getParameterMap(); + + for (Map.Entry<String,String[]> p : pmap.entrySet()) { + out.println("- " + p.getKey() + "=" + String.join(",", p.getValue())); + } + + out.println(RequestDispatcher.INCLUDE_REQUEST_URI + ": " + request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)); + out.println(RequestDispatcher.INCLUDE_CONTEXT_PATH + ": " + request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH)); + out.println(RequestDispatcher.INCLUDE_SERVLET_PATH + ": " + request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH)); + out.println(RequestDispatcher.INCLUDE_PATH_INFO + ": " + request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO)); + out.println(RequestDispatcher.INCLUDE_QUERY_STRING + ": " + request.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING)); + + return; + } + + response.sendError(404); + } +} diff --git a/test/java/include/index.html b/test/java/include/index.html new file mode 100644 index 00000000..4f5a6379 --- /dev/null +++ b/test/java/include/index.html @@ -0,0 +1 @@ +<html><body>This is index.html.</body></html> diff --git a/test/java/include/web.xml b/test/java/include/web.xml new file mode 100644 index 00000000..2ed86f1d --- /dev/null +++ b/test/java/include/web.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="utf-8" ?> + +<web-app xmlns="http://java.sun.com/xml/ns/j2ee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" + version="3.0"> + + <servlet> + <servlet-name>inc</servlet-name> + <servlet-class>app</servlet-class> + <init-param><param-name>id</param-name><param-value>inc</param-value></init-param> + </servlet> + + <servlet> + <servlet-name>data</servlet-name> + <servlet-class>app</servlet-class> + <init-param><param-name>id</param-name><param-value>data</param-value></init-param> + </servlet> + + <servlet-mapping> + <servlet-name>inc</servlet-name> + <url-pattern>/inc/*</url-pattern> + </servlet-mapping> + + <servlet-mapping> + <servlet-name>data</servlet-name> + <url-pattern>/data/*</url-pattern> + </servlet-mapping> + + <servlet-mapping> + <servlet-name>data</servlet-name> + <url-pattern>/WEB-INF/index.html</url-pattern> + <url-pattern>/index.html</url-pattern> + </servlet-mapping> + +</web-app> + |