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.PrintWriter;
    18  import java.util.Date;
    19  import java.util.Enumeration;
    20
    21  import javax.servlet.ServletConfig;
    22  import javax.servlet.ServletException;
    23  import javax.servlet.http.Cookie;
    24  import javax.servlet.http.HttpServlet;
    25  import javax.servlet.http.HttpServletRequest;
    26  import javax.servlet.http.HttpServletResponse;
    27  import javax.servlet.http.HttpSession;
    28
    29  import org.mortbay.util.StringUtil;
    30
    31
    32  /* ------------------------------------------------------------ */
    33  /** Test Servlet Cookies.
    34   *
    35   * @author Greg Wilkins (gregw)
    36   */
    37  public class CookieDump extends HttpServlet
    38  {
    39      int redirectCount=0;
    40
    41      /* ------------------------------------------------------------ */
    42      public void init(ServletConfig config)
    43           throws ServletException
    44      {
    45          super.init(config);
    46      }
    47
    48      /* ------------------------------------------------------------ */
    49      protected void handleForm(HttpServletRequest request,
    50                            HttpServletResponse response)
    51      {
    52          String action = request.getParameter("Action");
    53          String name =  request.getParameter("Name");
    54          String value =  request.getParameter("Value");
    55          String age =  request.getParameter("Age");
    56
    57          if (name!=null && name.length()>0)
    58          {
    59              Cookie cookie = new Cookie(name,value);
    60              if (age!=null && age.length()>0)
    61                  cookie.setMaxAge(Integer.parseInt(age));
    62              response.addCookie(cookie);
    63          }
    64      }
    65
    66      /* ------------------------------------------------------------ */
    67      public void doPost(HttpServletRequest request,
    68                         HttpServletResponse response)
    69          throws ServletException, IOException
    70      {
    71          handleForm(request,response);
    72          String nextUrl = getURI(request)+"?R="+redirectCount++;
    73          String encodedUrl=response.encodeRedirectURL(nextUrl);
    74          response.sendRedirect(encodedUrl);
    75      }
    76
    77      /* ------------------------------------------------------------ */
    78      public void doGet(HttpServletRequest request,
    79                        HttpServletResponse response)
    80          throws ServletException, IOException
    81      {
    82          handleForm(request,response);
    83
    84          response.setContentType("text/html");
    85
    86
    87          PrintWriter out = response.getWriter();
    88          out.println("<h1>Cookie Dump Servlet:</h1>");
    89
    90          Cookie[] cookies = request.getCookies();
    91
    92          for (int i=0;cookies!=null && i<cookies.length;i++)
    93          {
    94              out.println("<b>"+deScript(cookies[i].getName())+"</b>="+deScript(cookies[i].getValue())+"<br/>");
    95          }
    96
    97          out.println("<form action=\""+response.encodeURL(getURI(request))+"\" method=\"post\">");
    98
    99          out.println("<b>Name:</b><input type=\"text\" name=\"Name\" value=\"name\"/><br/>");
    100         out.println("<b>Value:</b><input type=\"text\" name=\"Value\" value=\"value\"/><br/>");
    101         out.println("<b>Max-Age:</b><input type=\"text\" name=\"Age\" value=\"60\"/><br/>");
    102         out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>");
    103
    104     }
    105
    106     /* ------------------------------------------------------------ */
    107     public String getServletInfo() {
    108         return "Session Dump Servlet";
    109     }
    110
    111     /* ------------------------------------------------------------ */
    112     private String getURI(HttpServletRequest request)
    113     {
    114         String uri=(String)request.getAttribute("javax.servlet.forward.request_uri");
    115         if (uri==null)
    116             uri=request.getRequestURI();
    117         return uri;
    118     }
    119
    120     /* ------------------------------------------------------------ */
    121     protected String deScript(String string)
    122     {
    123         if (string==null)
    124             return null;
    125         string=StringUtil.replace(string, "&", "&amp;");
    126         string=StringUtil.replace(string, "<", "&lt;");
    127         string=StringUtil.replace(string, ">", "&gt;");
    128         return string;
    129     }
    130 }