View Javadoc

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