KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > servlet > SessionDump


1 // ========================================================================
2
// $Id: SessionDump.java,v 1.15 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.servlet;
17 import java.io.IOException JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServlet JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.mortbay.log.LogFactory;
31 import org.mortbay.html.Page;
32 import org.mortbay.html.TableForm;
33 import org.mortbay.util.LogSupport;
34
35
36 /* ------------------------------------------------------------ */
37 /** Test Servlet Sessions.
38  *
39  * @version $Revision: 1.15 $
40  * @author Greg Wilkins (gregw)
41  */

42 public class SessionDump extends HttpServlet JavaDoc
43 {
44     private static Log log = LogFactory.getLog(SessionDump.class);
45
46     int redirectCount=0;
47     /* ------------------------------------------------------------ */
48     String JavaDoc pageType;
49
50     /* ------------------------------------------------------------ */
51     public void init(ServletConfig JavaDoc config)
52          throws ServletException JavaDoc
53     {
54         super.init(config);
55     }
56
57     /* ------------------------------------------------------------ */
58     public void doPost(HttpServletRequest JavaDoc request,
59                        HttpServletResponse JavaDoc response)
60         throws ServletException JavaDoc, IOException JavaDoc
61     {
62         HttpSession JavaDoc session = request.getSession(false);
63         String JavaDoc action = request.getParameter("Action");
64         String JavaDoc name = request.getParameter("Name");
65         String JavaDoc value = request.getParameter("Value");
66         String JavaDoc age = request.getParameter("MaxAge");
67
68         String JavaDoc nextUrl = getURI(request)+"?R="+redirectCount++;
69         if (action.equals("New Session"))
70         {
71             session = request.getSession(true);
72         }
73         else
74         if (session!=null)
75         {
76             if (action.equals("Invalidate"))
77                 session.invalidate();
78             else if (action.equals("Set"))
79             {
80                 session.setAttribute(name,value);
81                 try
82                 {
83                     int m = Integer.parseInt(age);
84                     session.setMaxInactiveInterval(m);
85                 }
86                 catch(Exception JavaDoc e)
87                 {
88                     LogSupport.ignore(log,e);
89                 }
90             }
91             else if (action.equals("Remove"))
92                 session.removeAttribute(name);
93         }
94
95         String JavaDoc encodedUrl=response.encodeRedirectURL(nextUrl);
96         response.sendRedirect(encodedUrl);
97         
98     }
99     
100         
101     /* ------------------------------------------------------------ */
102     public void doGet(HttpServletRequest JavaDoc request,
103                       HttpServletResponse JavaDoc response)
104         throws ServletException JavaDoc, IOException JavaDoc
105     {
106         response.setContentType("text/html");
107         Page page= new Page();
108
109         HttpSession JavaDoc session = request.getSession(getURI(request).indexOf("new")>0);
110         
111         page.title("Session Dump Servlet: ");
112         
113         TableForm tf = new TableForm(response.encodeURL(getURI(request)));
114         tf.method("POST");
115         
116         if (session==null)
117         {
118             page.add("<H1>No Session</H1>");
119             tf.addButton("Action","New Session");
120         }
121         else
122         {
123             try
124             {
125                 tf.addText("ID",session.getId());
126                 tf.addText("State",session.isNew()?"NEW":"Valid");
127                 tf.addText("Creation",
128                            new Date JavaDoc(session.getCreationTime()).toString());
129                 tf.addText("Last Access",
130                            new Date JavaDoc(session.getLastAccessedTime()).toString());
131                 tf.addText("Max Inactive",
132                            ""+session.getMaxInactiveInterval());
133
134                 tf.addText("Context",""+session.getServletContext());
135                 
136                 Enumeration JavaDoc keys=session.getAttributeNames();
137                 while(keys.hasMoreElements())
138                 {
139                     String JavaDoc name=(String JavaDoc)keys.nextElement();
140                     String JavaDoc value=session.getAttribute(name).toString();
141                     tf.addText(name,value);
142                 }
143                 
144                 tf.addTextField("Name","Property Name",20,"name");
145                 tf.addTextField("Value","Property Value",20,"value");
146                 tf.addTextField("MaxAge","MaxAge(s)",5,"");
147                 tf.addButtonArea();
148                 tf.addButton("Action","Set");
149                 tf.addButton("Action","Remove");
150                 tf.addButton("Action","Invalidate");
151
152                 page.add(tf);
153                 tf=null;
154                 if (request.isRequestedSessionIdFromCookie())
155                     page.add("<P>Turn off cookies in your browser to try url encoding<BR>");
156                 
157                 if (request.isRequestedSessionIdFromURL())
158                     page.add("<P>Turn on cookies in your browser to try cookie encoding<BR>");
159                 
160             }
161             catch (IllegalStateException JavaDoc e)
162             {
163                 log.debug(LogSupport.EXCEPTION,e);
164                 page.add("<H1>INVALID Session</H1>");
165                 tf=new TableForm(getURI(request));
166                 tf.addButton("Action","New Session");
167             }
168         }
169
170         if (tf!=null)
171             page.add(tf);
172         
173         Writer JavaDoc writer=response.getWriter();
174         page.write(writer);
175         writer.flush();
176     }
177
178     /* ------------------------------------------------------------ */
179     public String JavaDoc getServletInfo() {
180         return "Session Dump Servlet";
181     }
182
183     /* ------------------------------------------------------------ */
184     private String JavaDoc getURI(HttpServletRequest JavaDoc request)
185     {
186         String JavaDoc uri=(String JavaDoc)request.getAttribute("javax.servlet.forward.request_uri");
187         if (uri==null)
188             uri=request.getRequestURI();
189         return uri;
190     }
191     
192 }
193
Popular Tags