1 package com.quadcap.http.servlets.jsp; 2 3 40 41 import java.io.IOException ; 42 import java.io.PrintWriter ; 43 44 import java.util.Enumeration ; 45 import java.util.Hashtable ; 46 import java.util.Vector ; 47 48 import javax.servlet.RequestDispatcher ; 49 import javax.servlet.Servlet ; 50 import javax.servlet.ServletConfig ; 51 import javax.servlet.ServletContext ; 52 import javax.servlet.ServletException ; 53 import javax.servlet.ServletRequest ; 54 import javax.servlet.ServletResponse ; 55 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 import javax.servlet.http.HttpSession ; 59 60 import com.quadcap.http.server22.HttpResponse; 61 62 import com.quadcap.util.Debug; 63 64 69 public class PageContext extends javax.servlet.jsp.PageContext { 70 Hashtable pageAttributes = null; 71 Servlet servlet; 72 HttpServletRequest request; 73 HttpServletResponse response; 74 HttpSession session; 75 String errorPageURL; 76 boolean needsSession; 77 int bufferSize; 78 boolean autoFlush; 79 javax.servlet.jsp.JspWriter out; 80 81 public void initialize(Servlet servlet, ServletRequest request, 82 ServletResponse response, 83 String errorPageURL, boolean needsSession, 84 int bufferSize, 85 boolean autoFlush) 86 throws IOException , IllegalStateException , IllegalArgumentException 87 { 88 this.servlet = servlet; 89 this.request = (HttpServletRequest )request; 90 this.response = (HttpServletResponse )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 name, Object val) { 114 setAttribute(name, val, PAGE_SCOPE); 115 } 116 117 public void setAttribute(String name, Object val, int scope) { 118 switch (scope) { 119 case PAGE_SCOPE: 120 if (pageAttributes == null) pageAttributes = new Hashtable (); 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 getAttribute(String name) { 135 return getAttribute(name, PAGE_SCOPE); 136 } 137 138 public Object getAttribute(String 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 findAttribute(String name) { 155 Object 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 name) { 163 removeAttribute(name, PAGE_SCOPE); 164 } 165 166 public void removeAttribute(String 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, ""); 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 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 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 v = new Vector (); 201 if (session != null) { 202 String [] 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 ().elements(); 210 } 211 212 public javax.servlet.jsp.JspWriter getOut() { 213 return out; 214 } 215 216 public HttpSession getSession() { 217 if (session == null) session = new JspSession(request); 218 return session; 219 } 220 221 public Object getPage() { 222 return servlet; 223 } 224 225 public ServletRequest getRequest() { 226 return request; 227 } 228 229 public ServletResponse getResponse() { 230 return response; 231 } 232 233 public Exception getException() { 234 return (Exception )request.getAttribute("exception"); 235 } 236 237 public ServletConfig getServletConfig() { 238 return servlet.getServletConfig(); 239 } 240 241 public ServletContext getServletContext() { 242 return getServletConfig().getServletContext(); 243 } 244 245 final String relativize(String url) throws ServletException { 246 if (url.charAt(0) != '/') { 247 String 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 url) throws ServletException , IOException { 257 url = relativize(url); 258 RequestDispatcher rd = getServletContext().getRequestDispatcher(url); 259 out.clearBuffer(); 260 rd.forward(request, response); 261 } 262 263 public void include(String url) throws ServletException , IOException { 264 url = relativize(url); 265 RequestDispatcher rd = getServletContext().getRequestDispatcher(url); 266 rd.include(request, response); 267 } 268 269 public void handleFinally() throws IOException { 270 if (autoFlush) { 271 out.flush(); 272 } 273 } 274 275 public void handlePageException(Exception e) 276 throws ServletException , IOException 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 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 |