View Javadoc

    1   // ========================================================================
    2   // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package com.acme;
    16  import java.io.IOException;
    17  import java.io.OutputStream;
    18  import java.io.PrintWriter;
    19
    20  import javax.servlet.RequestDispatcher;
    21  import javax.servlet.ServletConfig;
    22  import javax.servlet.ServletContext;
    23  import javax.servlet.ServletException;
    24  import javax.servlet.ServletOutputStream;
    25  import javax.servlet.http.HttpServlet;
    26  import javax.servlet.http.HttpServletRequest;
    27  import javax.servlet.http.HttpServletRequestWrapper;
    28  import javax.servlet.http.HttpServletResponse;
    29  import javax.servlet.http.HttpServletResponseWrapper;
    30
    31  /* ------------------------------------------------------------ */
    32  /** Test Servlet RequestDispatcher.
    33   *
    34   * @author Greg Wilkins (gregw)
    35   */
    36  public class DispatchServlet extends HttpServlet
    37  {
    38      /* ------------------------------------------------------------ */
    39      String pageType;
    40
    41      /* ------------------------------------------------------------ */
    42      public void init(ServletConfig config) throws ServletException
    43      {
    44          super.init(config);
    45      }
    46
    47      /* ------------------------------------------------------------ */
    48      public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
    49      {
    50          doGet(sreq, sres);
    51      }
    52
    53      /* ------------------------------------------------------------ */
    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                     out.println("IOException");
    156                     throw new IllegalStateException();
    157                 }
    158                 catch(IOException e)
    159                 {}
    160             }
    161             else
    162             {
    163                 sres.setContentType("text/html");
    164                 PrintWriter pout= sres.getWriter();
    165                 pout.write("<H1>No dispatcher for: " + info + "</H1><HR>");
    166                 pout.flush();
    167             }
    168         }
    169         else if (info.startsWith("/forwardC/"))
    170         {
    171             info= info.substring(9);
    172             if (info.indexOf('?') < 0)
    173                 info += "?Dispatch=forward";
    174             else
    175                 info += "&Dispatch=forward";
    176
    177             String cpath= info.substring(0, info.indexOf('/', 1));
    178             info= info.substring(cpath.length());
    179
    180             ServletContext context= getServletContext().getContext(cpath);
    181             RequestDispatcher dispatch= context.getRequestDispatcher(info);
    182
    183             if (dispatch != null)
    184             {
    185                 dispatch.forward(sreq, sres);
    186             }
    187             else
    188             {
    189                 sres.setContentType("text/html");
    190                 PrintWriter pout= sres.getWriter();
    191                 pout.write("<H1>No dispatcher for: " + cpath + "/" + info + "</H1><HR>");
    192                 pout.flush();
    193             }
    194         }
    195         else if (info.startsWith("/includeN/"))
    196         {
    197             sres.setContentType("text/html");
    198             info= info.substring(10);
    199             if (info.indexOf("/") >= 0)
    200                 info= info.substring(0, info.indexOf("/"));
    201
    202             PrintWriter pout;
    203             if (info.startsWith("/null"))
    204                 info= info.substring(5);
    205             else
    206             {
    207                 pout= sres.getWriter();
    208                 pout.write("<H1>Include named: " + info + "</H1><HR>");
    209             }
    210
    211             RequestDispatcher dispatch= getServletContext().getNamedDispatcher(info);
    212             if (dispatch != null)
    213                 dispatch.include(sreq, sres);
    214             else
    215             {
    216                 pout= sres.getWriter();
    217                 pout.write("<H1>No servlet named: " + info + "</H1>");
    218             }
    219
    220             pout= sres.getWriter();
    221             pout.write("<HR><H1>Included ");
    222         }
    223         else if (info.startsWith("/forwardN/"))
    224         {
    225             info= info.substring(10);
    226             if (info.indexOf("/") >= 0)
    227                 info= info.substring(0, info.indexOf("/"));
    228             RequestDispatcher dispatch= getServletContext().getNamedDispatcher(info);
    229             if (dispatch != null)
    230                 dispatch.forward(sreq, sres);
    231             else
    232             {
    233                 sres.setContentType("text/html");
    234                 PrintWriter pout= sres.getWriter();
    235                 pout.write("<H1>No servlet named: " + info + "</H1>");
    236                 pout.flush();
    237             }
    238         }
    239         else
    240         {
    241             sres.setContentType("text/html");
    242             PrintWriter pout= sres.getWriter();
    243             pout.write(
    244                     "<H1>Dispatch URL must be of the form: </H1>"
    245                     + "<PRE>"
    246                     + prefix
    247                     + "/includeW/path\n"
    248                     + prefix
    249                     + "/includeS/path\n"
    250                     + prefix
    251                     + "/forward/path\n"
    252                     + prefix
    253                     + "/includeN/name\n"
    254                     + prefix
    255                     + "/forwardC/_context/path\n</PRE>");
    256         }
    257
    258     }
    259
    260     /* ------------------------------------------------------------ */
    261     public String getServletInfo()
    262     {
    263         return "Include Servlet";
    264     }
    265
    266     /* ------------------------------------------------------------ */
    267     public synchronized void destroy()
    268     {
    269     }
    270
    271 }