KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > jsp > PageContext


1 package com.quadcap.http.servlets.jsp;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42 import java.io.PrintWriter JavaDoc;
43
44 import java.util.Enumeration JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 import javax.servlet.RequestDispatcher JavaDoc;
49 import javax.servlet.Servlet JavaDoc;
50 import javax.servlet.ServletConfig JavaDoc;
51 import javax.servlet.ServletContext JavaDoc;
52 import javax.servlet.ServletException JavaDoc;
53 import javax.servlet.ServletRequest JavaDoc;
54 import javax.servlet.ServletResponse JavaDoc;
55
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57 import javax.servlet.http.HttpServletResponse JavaDoc;
58 import javax.servlet.http.HttpSession JavaDoc;
59
60 import com.quadcap.http.server22.HttpResponse;
61
62 import com.quadcap.util.Debug;
63
64 /**
65  * The PageContext implementation.
66  *
67  * @author Stan Bailes
68  */

69 public class PageContext extends javax.servlet.jsp.PageContext JavaDoc {
70     Hashtable JavaDoc pageAttributes = null;
71     Servlet JavaDoc servlet;
72     HttpServletRequest JavaDoc request;
73     HttpServletResponse JavaDoc response;
74     HttpSession JavaDoc session;
75     String JavaDoc errorPageURL;
76     boolean needsSession;
77     int bufferSize;
78     boolean autoFlush;
79     javax.servlet.jsp.JspWriter JavaDoc out;
80
81     public void initialize(Servlet JavaDoc servlet, ServletRequest JavaDoc request,
82                ServletResponse JavaDoc response,
83                String JavaDoc errorPageURL, boolean needsSession,
84                int bufferSize,
85                boolean autoFlush)
86     throws IOException JavaDoc, IllegalStateException JavaDoc, IllegalArgumentException JavaDoc
87     {
88     this.servlet = servlet;
89     this.request = (HttpServletRequest JavaDoc)request;
90     this.response = (HttpServletResponse JavaDoc)response;
91     this.errorPageURL = errorPageURL;
92     this.needsSession = needsSession;
93     this.bufferSize = bufferSize;
94     this.autoFlush = autoFlush;
95     this.session = null;
96
97         if (response instanceof HttpResponse) {
98             HttpResponse qresp = (HttpResponse)response;
99             this.out = qresp.getJspWriter(bufferSize, autoFlush);
100         } else {
101             this.out = new JspWriter(response.getWriter(), bufferSize, autoFlush);
102         }
103     }
104
105     public void release() {
106     this.servlet = null;
107     this.request = null;
108     this.response = null;
109     this.errorPageURL = null;
110     this.session = null;
111     }
112
113     public void setAttribute(String JavaDoc name, Object JavaDoc val) {
114     setAttribute(name, val, PAGE_SCOPE);
115     }
116
117     public void setAttribute(String JavaDoc name, Object JavaDoc val, int scope) {
118     switch (scope) {
119     case PAGE_SCOPE:
120         if (pageAttributes == null) pageAttributes = new Hashtable JavaDoc();
121         pageAttributes.put(name, val);
122         break;
123     case REQUEST_SCOPE:
124         request.setAttribute(name, val);
125         break;
126     case SESSION_SCOPE:
127         if (session != null) session.putValue(name, val);
128         break;
129     case APPLICATION_SCOPE:
130         getServletContext().setAttribute(name, val);
131     }
132     }
133
134     public Object JavaDoc getAttribute(String JavaDoc name) {
135     return getAttribute(name, PAGE_SCOPE);
136     }
137
138     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
139     switch (scope) {
140     case PAGE_SCOPE:
141         if (pageAttributes != null) return pageAttributes.get(name);
142         break;
143     case REQUEST_SCOPE:
144         return request.getAttribute(name);
145     case SESSION_SCOPE:
146         if (session != null) return session.getValue(name);
147         break;
148     case APPLICATION_SCOPE:
149         return getServletContext().getAttribute(name);
150     }
151     return null;
152     }
153
154     public Object JavaDoc findAttribute(String JavaDoc name) {
155     Object JavaDoc obj = null;
156     for (int i = 0; obj == null && i < 4; i++) {
157         obj = getAttribute(name, i);
158     }
159     return obj;
160     }
161
162     public void removeAttribute(String JavaDoc name) {
163     removeAttribute(name, PAGE_SCOPE);
164     }
165
166     public void removeAttribute(String JavaDoc name, int scope) {
167     switch (scope) {
168     case PAGE_SCOPE:
169         if (pageAttributes != null) pageAttributes.remove(name);
170         break;
171     case REQUEST_SCOPE:
172         request.setAttribute(name, ""); // XXX no remove!
173
break;
174     case SESSION_SCOPE:
175         if (session != null) session.removeValue(name);
176         break;
177     case APPLICATION_SCOPE:
178         getServletContext().removeAttribute(name);
179     }
180     }
181
182     public int getAttributesScope(String JavaDoc name) {
183     int scope = 0;
184     for (int i = 0; scope == 0 && i < 4; i++) {
185         if (getAttribute(name, i) != null) {
186         scope = i+1;
187         }
188     }
189     return scope;
190     }
191
192     public Enumeration JavaDoc getAttributeNamesInScope(int scope) {
193     switch (scope) {
194     case PAGE_SCOPE:
195         if (pageAttributes != null) return pageAttributes.keys();
196         break;
197     case REQUEST_SCOPE:
198         return request.getAttributeNames();
199     case SESSION_SCOPE:
200         Vector JavaDoc v = new Vector JavaDoc();
201         if (session != null) {
202         String JavaDoc[] s = session.getValueNames();
203         for (int i = 0; i < s.length; i++) v.addElement(s[i]);
204         }
205         return v.elements();
206     case APPLICATION_SCOPE:
207         return getServletContext().getAttributeNames();
208     }
209     return new Vector JavaDoc().elements();
210     }
211
212     public javax.servlet.jsp.JspWriter JavaDoc getOut() {
213     return out;
214     }
215
216     public HttpSession JavaDoc getSession() {
217     if (session == null) session = new JspSession(request);
218     return session;
219     }
220
221     public Object JavaDoc getPage() {
222     return servlet;
223     }
224
225     public ServletRequest JavaDoc getRequest() {
226     return request;
227     }
228
229     public ServletResponse JavaDoc getResponse() {
230     return response;
231     }
232
233     public Exception JavaDoc getException() {
234     return (Exception JavaDoc)request.getAttribute("exception");
235     }
236
237     public ServletConfig JavaDoc getServletConfig() {
238     return servlet.getServletConfig();
239     }
240
241     public ServletContext JavaDoc getServletContext() {
242     return getServletConfig().getServletContext();
243     }
244
245     final String JavaDoc relativize(String JavaDoc url) throws ServletException JavaDoc {
246     if (url.charAt(0) != '/') {
247         String JavaDoc p = request.getServletPath();
248         int idx = p.lastIndexOf('/');
249         if (idx >= 0) {
250         url = p.substring(0, idx) + "/" + url;
251         }
252     }
253     return url;
254     }
255
256     public void forward(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
257     url = relativize(url);
258     RequestDispatcher JavaDoc rd = getServletContext().getRequestDispatcher(url);
259     out.clearBuffer();
260     rd.forward(request, response);
261     }
262
263     public void include(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
264     url = relativize(url);
265     RequestDispatcher JavaDoc rd = getServletContext().getRequestDispatcher(url);
266     rd.include(request, response);
267     }
268
269     public void handleFinally() throws IOException JavaDoc {
270         if (autoFlush) {
271             out.flush();
272         }
273     }
274
275     public void handlePageException(Exception JavaDoc e)
276     throws ServletException JavaDoc, IOException JavaDoc
277     {
278     if (errorPageURL == null) {
279         if (Trace.level() > 1) {
280         Debug.print(e);
281         }
282         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
283         PrintWriter JavaDoc w = response.getWriter();
284         w.println("<html><head><title>");
285         w.println(e.toString());
286         w.println("</title></head><body>");
287         w.println("<pre>");
288         e.printStackTrace(w);
289         w.println("</pre></body></html>");
290         w.flush();
291     } else {
292         request.setAttribute("exception", e);
293         forward(errorPageURL);
294     }
295     }
296 }
297
Popular Tags