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.HttpServlet;
    24  import javax.servlet.http.HttpServletRequest;
    25  import javax.servlet.http.HttpServletResponse;
    26  import javax.servlet.http.HttpSession;
    27
    28
    29  /* ------------------------------------------------------------ */
    30  /** Test Servlet Sessions.
    31   *
    32   * @author Greg Wilkins (gregw)
    33   */
    34  public class SessionDump extends HttpServlet
    35  {
    36
    37      int redirectCount=0;
    38      /* ------------------------------------------------------------ */
    39      String pageType;
    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          HttpSession session = request.getSession(false);
    53          String action = request.getParameter("Action");
    54          String name =  request.getParameter("Name");
    55          String value =  request.getParameter("Value");
    56
    57          if (action!=null)
    58          {
    59              if(action.equals("New Session"))
    60              {
    61                  session = request.getSession(true);
    62                  session.setAttribute("test","value");
    63              }
    64              else if (session!=null)
    65              {
    66                  if (action.equals("Invalidate"))
    67                      session.invalidate();
    68                  else if (action.equals("Set") && name!=null && name.length()>0)
    69                      session.setAttribute(name,value);
    70                  else if (action.equals("Remove"))
    71                      session.removeAttribute(name);
    72              }
    73          }
    74      }
    75
    76      /* ------------------------------------------------------------ */
    77      public void doPost(HttpServletRequest request,
    78                         HttpServletResponse response)
    79          throws ServletException, IOException
    80      {
    81          handleForm(request,response);
    82          String nextUrl = getURI(request)+"?R="+redirectCount++;
    83          String encodedUrl=response.encodeRedirectURL(nextUrl);
    84          response.sendRedirect(encodedUrl);
    85      }
    86
    87      /* ------------------------------------------------------------ */
    88      public void doGet(HttpServletRequest request,
    89                        HttpServletResponse response)
    90          throws ServletException, IOException
    91      {
    92          handleForm(request,response);
    93
    94          response.setContentType("text/html");
    95
    96          HttpSession session = request.getSession(getURI(request).indexOf("new")>0);
    97          try
    98          {
    99              if (session!=null)
    100                 session.isNew();
    101         }
    102         catch(IllegalStateException e)
    103         {
    104             session=null;
    105         }
    106
    107         PrintWriter out = response.getWriter();
    108         out.println("<h1>Session Dump Servlet:</h1>");
    109         out.println("<form action=\""+response.encodeURL(getURI(request))+"\" method=\"post\">");
    110
    111         if (session==null)
    112         {
    113             out.println("<H3>No Session</H3>");
    114             out.println("<input type=\"submit\" name=\"Action\" value=\"New Session\"/>");
    115         }
    116         else
    117         {
    118             try
    119             {
    120                 out.println("<b>ID:</b> "+session.getId()+"<br/>");
    121                 out.println("<b>New:</b> "+session.isNew()+"<br/>");
    122                 out.println("<b>Created:</b> "+new Date(session.getCreationTime())+"<br/>");
    123                 out.println("<b>Last:</b> "+new Date(session.getLastAccessedTime())+"<br/>");
    124                 out.println("<b>Max Inactive:</b> "+session.getMaxInactiveInterval()+"<br/>");
    125                 out.println("<b>Context:</b> "+session.getServletContext()+"<br/>");
    126
    127
    128                 Enumeration keys=session.getAttributeNames();
    129                 while(keys.hasMoreElements())
    130                 {
    131                     String name=(String)keys.nextElement();
    132                     String value=""+session.getAttribute(name);
    133
    134                     out.println("<b>"+name+":</b> "+value+"<br/>");
    135                 }
    136
    137                 out.println("<b>Name:</b><input type=\"text\" name=\"Name\" /><br/>");
    138                 out.println("<b>Value:</b><input type=\"text\" name=\"Value\" /><br/>");
    139
    140                 out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>");
    141                 out.println("<input type=\"submit\" name=\"Action\" value=\"Remove\"/>");
    142                 out.println("<input type=\"submit\" name=\"Action\" value=\"Invalidate\"/><br/>");
    143
    144                 out.println("</form><br/>");
    145
    146                 if (request.isRequestedSessionIdFromCookie())
    147                     out.println("<P>Turn off cookies in your browser to try url encoding<BR>");
    148
    149                 if (request.isRequestedSessionIdFromURL())
    150                     out.println("<P>Turn on cookies in your browser to try cookie encoding<BR>");
    151                 out.println("<a href=\""+response.encodeURL(request.getRequestURI()+"?q=0")+"\">Encoded Link</a><BR>");
    152
    153             }
    154             catch (IllegalStateException e)
    155             {
    156                 e.printStackTrace();
    157             }
    158         }
    159
    160     }
    161
    162     /* ------------------------------------------------------------ */
    163     public String getServletInfo() {
    164         return "Session Dump Servlet";
    165     }
    166
    167     /* ------------------------------------------------------------ */
    168     private String getURI(HttpServletRequest request)
    169     {
    170         String uri=(String)request.getAttribute("javax.servlet.forward.request_uri");
    171         if (uri==null)
    172             uri=request.getRequestURI();
    173         return uri;
    174     }
    175
    176 }