View Javadoc

1   // ========================================================================
2   // Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package com.acme;
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.io.PrintWriter;
18  
19  import javax.servlet.RequestDispatcher;
20  import javax.servlet.ServletContext;
21  import javax.servlet.ServletException;
22  import javax.servlet.ServletOutputStream;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletRequestWrapper;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpServletResponseWrapper;
28  
29  import org.eclipse.jetty.util.log.Log;
30  
31  
32  /* ------------------------------------------------------------ */
33  /** Test Servlet RequestDispatcher.
34   * 
35   * 
36   */
37  public class DispatchServlet extends HttpServlet
38  {
39      /* ------------------------------------------------------------ */
40      String pageType;
41  
42      /* ------------------------------------------------------------ */
43      @Override
44      public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
45      {
46          doGet(sreq, sres);
47      }
48  
49      /* ------------------------------------------------------------ */
50      @Override
51      public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
52      {
53          if (sreq.getParameter("wrap") != null)
54          {
55              sreq= new HttpServletRequestWrapper(sreq);
56              sres= new HttpServletResponseWrapper(sres);
57          }
58          
59          if (sreq.getParameter("session") != null)
60              sreq.getSession(true);
61  
62          String prefix=
63              sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath();
64              
65          String info;
66          
67          if (sreq.getAttribute("javax.servlet.include.servlet_path") != null)
68              info= (String)sreq.getAttribute("javax.servlet.include.path_info");
69          else
70              info= sreq.getPathInfo();
71          
72          if (info == null)
73              info= "NULL";
74  
75          if (info.indexOf(sreq.getServletPath()) > 0)
76          {
77              sres.sendError(403,"Nested " + sreq.getServletPath() + " forbidden.");
78              return;
79          }
80          
81          if(info.indexOf(getServletName()) > 0)
82          {
83              sres.sendError(403,"Nested " + getServletName() + " forbidden.");
84              return;
85          }
86  
87          if (info.startsWith("/includeW/"))
88          {
89              sres.setContentType("text/html");
90              info= info.substring(9);
91              if (info.indexOf('?') < 0)
92                  info += "?Dispatch=include";
93              else
94                  info += "&Dispatch=include";
95              
96              PrintWriter pout= null;
97              pout= sres.getWriter();
98              pout.write("<H1>Include (writer): " + info + "</H1><HR>");
99              
100             RequestDispatcher dispatch= getServletContext().getRequestDispatcher(info);
101             if (dispatch == null)
102             {
103                 pout= sres.getWriter();
104                 pout.write("<H1>Null dispatcher</H1>");
105             }
106             else
107                 dispatch.include(sreq, sres);
108             
109             pout.write("<HR><H1>-- Included (writer)</H1>");
110         }
111         else if (info.startsWith("/includeS/"))
112         {
113             sres.setContentType("text/html");
114             info= info.substring(9);
115             if (info.indexOf('?') < 0)
116                 info += "?Dispatch=include";
117             else
118                 info += "&Dispatch=include";
119             
120             OutputStream out= null;
121             out= sres.getOutputStream();
122             out.write(("<H1>Include (outputstream): " + info + "</H1><HR>").getBytes());
123             
124             RequestDispatcher dispatch= getServletContext().getRequestDispatcher(info);
125             if (dispatch == null)
126             {
127                 out= sres.getOutputStream();
128                 out.write("<H1>Null dispatcher</H1>".getBytes());
129             }
130             else
131                 dispatch.include(sreq, sres);
132             
133             out.write("<HR><H1>-- Included (outputstream)</H1>".getBytes());
134             
135         }
136         else if (info.startsWith("/forward/"))
137         {
138             info= info.substring(8);
139             if (info.indexOf('?') < 0)
140                 info += "?Dispatch=forward";
141             else
142                 info += "&Dispatch=forward";
143             
144             RequestDispatcher dispatch= getServletContext().getRequestDispatcher(info);
145             if (dispatch != null)
146             {
147                 ServletOutputStream out =sres.getOutputStream();
148                 out.print("Can't see this");
149                 dispatch.forward(sreq, sres);
150                 try
151                 {
152                     // should be closed
153                     out.println("IOException");
154                     // should not get here
155                     throw new IllegalStateException();
156                 }
157                 catch(IOException e)
158                 {
159                     Log.ignore(e);
160                 }
161             }
162             else
163             {
164                 sres.setContentType("text/html");
165                 PrintWriter pout= sres.getWriter();
166                 pout.write("<H1>No dispatcher for: " + info + "</H1><HR>");
167                 pout.flush();
168             }
169         }
170         else if (info.startsWith("/forwardC/"))
171         {
172             info= info.substring(9);
173             if (info.indexOf('?') < 0)
174                 info += "?Dispatch=forward";
175             else
176                 info += "&Dispatch=forward";
177             
178             String cpath= info.substring(0, info.indexOf('/', 1));
179             info= info.substring(cpath.length());
180             
181             ServletContext context= getServletContext().getContext(cpath);
182             RequestDispatcher dispatch= context.getRequestDispatcher(info);
183             
184             if (dispatch != null)
185             {
186                 dispatch.forward(sreq, sres);
187             }
188             else
189             {
190                 sres.setContentType("text/html");
191                 PrintWriter pout= sres.getWriter();
192                 pout.write("<H1>No dispatcher for: " + cpath + "/" + info + "</H1><HR>");
193                 pout.flush();
194             }
195         }
196         else if (info.startsWith("/includeN/"))
197         {
198             sres.setContentType("text/html");
199             info= info.substring(10);
200             if (info.indexOf("/") >= 0)
201                 info= info.substring(0, info.indexOf("/"));
202             
203             PrintWriter pout;
204             if (info.startsWith("/null"))
205                 info= info.substring(5);
206             else
207             {
208                 pout= sres.getWriter();
209                 pout.write("<H1>Include named: " + info + "</H1><HR>");
210             }
211             
212             RequestDispatcher dispatch= getServletContext().getNamedDispatcher(info);
213             if (dispatch != null)
214                 dispatch.include(sreq, sres);
215             else
216             {
217                 pout= sres.getWriter();
218                 pout.write("<H1>No servlet named: " + info + "</H1>");
219             }
220             
221             pout= sres.getWriter();
222             pout.write("<HR><H1>Included ");
223         }
224         else if (info.startsWith("/forwardN/"))
225         {
226             info= info.substring(10);
227             if (info.indexOf("/") >= 0)
228                 info= info.substring(0, info.indexOf("/"));
229             RequestDispatcher dispatch= getServletContext().getNamedDispatcher(info);
230             if (dispatch != null)
231                 dispatch.forward(sreq, sres);
232             else
233             {
234                 sres.setContentType("text/html");
235                 PrintWriter pout= sres.getWriter();
236                 pout.write("<H1>No servlet named: " + info + "</H1>");
237                 pout.flush();
238             }
239         }
240         else
241         {
242             sres.setContentType("text/html");
243             PrintWriter pout= sres.getWriter();
244             pout.write(
245                     "<H1>Dispatch URL must be of the form: </H1>"
246                     + "<PRE>"
247                     + prefix
248                     + "/includeW/path\n"
249                     + prefix
250                     + "/includeS/path\n"
251                     + prefix
252                     + "/forward/path\n"
253                     + prefix
254                     + "/includeN/name\n"
255                     + prefix
256                     + "/forwardC/_context/path\n</PRE>");
257         }
258         
259     }
260 
261     /* ------------------------------------------------------------ */
262     @Override
263     public String getServletInfo()
264     {
265         return "Include Servlet";
266     }
267 
268     /* ------------------------------------------------------------ */
269     @Override
270     public synchronized void destroy()
271     {
272     }
273 
274 }