1 package com.diasparsoftware.javax.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.*;
7
8 /***
9 * A servlet that forwards to the location you specify. Use
10 * this in conjunction with ServletUnit (http://httpunit.sourceforge.net)
11 * to test page templates, such as JSPs.
12 *
13 * Sample use.
14 *
15 * <pre>
16 * servletRunner = new ServletRunner(
17 * getWebContentPath("/WEB-INF/web.xml"),
18 *
19 * servletRunner.registerServlet("/forward",
20 * ForwardingServlet.class.getName());
21 *
22 * client = servletRunner.newClient();
23 *
24 * ForwardingServlet servlet =
25 * (ForwardingServlet) invocationContext.getServlet();
26 *
27 * servlet.setForwardUri("/shopcart.jsp");
28 *
29 * HttpServletRequest request = invocationContext.getRequest();
30 * request.setAttribute("shopcartDisplay", shopcartBean);
31 *
32 * servlet.service(
33 * invocationContext.getRequest(),
34 * invocationContext.getResponse());
35 *
36 * WebResponse response =
37 * invocationContext.getServletResponse();
38 *
39 * assertEquals("....", response.getText());
40 * </pre>
41 *
42 * @version $Revision: 1.1 $
43 * @author <a href="jbr@diasparsoftware.com>J. B. Rainsberger</a>
44 */
45 public class ForwardingServlet extends HttpServlet {
46 private String forwardUri = "";
47
48 protected void doGet(
49 HttpServletRequest request,
50 HttpServletResponse response)
51 throws ServletException, IOException {
52
53 handleRequest(request, response);
54 }
55
56 protected void doPost(
57 HttpServletRequest request,
58 HttpServletResponse response)
59 throws ServletException, IOException {
60
61 handleRequest(request, response);
62 }
63
64 protected void handleRequest(
65 HttpServletRequest request,
66 HttpServletResponse response)
67 throws ServletException, IOException {
68
69 getServletContext().getRequestDispatcher(
70 getForwardUri()).forward(
71 request,
72 response);
73 }
74
75 /***
76 * The URI to which this servlet ought to forward when
77 * you invoke <code>service()</code>.
78 *
79 * @param forwardUri
80 */
81 public void setForwardUri(String forwardUri) {
82 this.forwardUri = forwardUri;
83 }
84
85 public String getForwardUri() {
86 return forwardUri;
87 }
88 }