View Javadoc

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