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