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