1 17 18 package org.apache.jasper.servlet; 19 20 import java.io.IOException ; 21 import java.lang.reflect.Constructor ; 22 import java.util.Enumeration ; 23 24 import javax.servlet.ServletConfig ; 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.apache.PeriodicEventListener; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.jasper.Constants; 36 import org.apache.jasper.EmbeddedServletOptions; 37 import org.apache.jasper.Options; 38 import org.apache.jasper.compiler.JspRuntimeContext; 39 import org.apache.jasper.compiler.Localizer; 40 41 57 public class JspServlet extends HttpServlet implements PeriodicEventListener { 58 59 private Log log = LogFactory.getLog(JspServlet.class); 61 62 private ServletContext context; 63 private ServletConfig config; 64 private Options options; 65 private JspRuntimeContext rctxt; 66 67 68 71 public void init(ServletConfig config) throws ServletException { 72 73 super.init(config); 74 this.config = config; 75 this.context = config.getServletContext(); 76 77 String engineOptionsName = 80 config.getInitParameter("engineOptionsClass"); 81 if (engineOptionsName != null) { 82 try { 84 ClassLoader loader = Thread.currentThread() 85 .getContextClassLoader(); 86 Class engineOptionsClass = loader.loadClass(engineOptionsName); 87 Class [] ctorSig = { ServletConfig .class, ServletContext .class }; 88 Constructor ctor = engineOptionsClass.getConstructor(ctorSig); 89 Object [] args = { config, context }; 90 options = (Options) ctor.newInstance(args); 91 } catch (Throwable e) { 92 log.warn("Failed to load engineOptionsClass", e); 94 options = new EmbeddedServletOptions(config, context); 96 } 97 } else { 98 options = new EmbeddedServletOptions(config, context); 100 } 101 rctxt = new JspRuntimeContext(context, options); 102 103 if (log.isDebugEnabled()) { 104 log.debug(Localizer.getMessage("jsp.message.scratch.dir.is", 105 options.getScratchDir().toString())); 106 log.debug(Localizer.getMessage("jsp.message.dont.modify.servlets")); 107 } 108 } 109 110 111 121 public int getJspCount() { 122 return this.rctxt.getJspCount(); 123 } 124 125 126 131 public void setJspReloadCount(int count) { 132 this.rctxt.setJspReloadCount(count); 133 } 134 135 136 144 public int getJspReloadCount() { 145 return this.rctxt.getJspReloadCount(); 146 } 147 148 149 162 boolean preCompile(HttpServletRequest request) throws ServletException { 163 164 String queryString = request.getQueryString(); 165 if (queryString == null) { 166 return (false); 167 } 168 int start = queryString.indexOf(Constants.PRECOMPILE); 169 if (start < 0) { 170 return (false); 171 } 172 queryString = 173 queryString.substring(start + Constants.PRECOMPILE.length()); 174 if (queryString.length() == 0) { 175 return (true); } 177 if (queryString.startsWith("&")) { 178 return (true); } 180 if (!queryString.startsWith("=")) { 181 return (false); } 183 int limit = queryString.length(); 184 int ampersand = queryString.indexOf("&"); 185 if (ampersand > 0) { 186 limit = ampersand; 187 } 188 String value = queryString.substring(1, limit); 189 if (value.equals("true")) { 190 return (true); } else if (value.equals("false")) { 192 return (true); } else { 199 throw new ServletException ("Cannot have request parameter " + 200 Constants.PRECOMPILE + " set to " + 201 value); 202 } 203 204 } 205 206 207 public void service (HttpServletRequest request, 208 HttpServletResponse response) 209 throws ServletException , IOException { 210 211 String jspUri = null; 212 213 String jspFile = (String ) request.getAttribute(Constants.JSP_FILE); 214 if (jspFile != null) { 215 jspUri = jspFile; 217 } else { 218 222 jspUri = (String ) request.getAttribute(Constants.INC_SERVLET_PATH); 223 if (jspUri != null) { 224 229 String pathInfo = (String ) request.getAttribute( 230 "javax.servlet.include.path_info"); 231 if (pathInfo != null) { 232 jspUri += pathInfo; 233 } 234 } else { 235 240 jspUri = request.getServletPath(); 241 String pathInfo = request.getPathInfo(); 242 if (pathInfo != null) { 243 jspUri += pathInfo; 244 } 245 } 246 } 247 248 if (log.isDebugEnabled()) { 249 log.debug("JspEngine --> " + jspUri); 250 log.debug("\t ServletPath: " + request.getServletPath()); 251 log.debug("\t PathInfo: " + request.getPathInfo()); 252 log.debug("\t RealPath: " + context.getRealPath(jspUri)); 253 log.debug("\t RequestURI: " + request.getRequestURI()); 254 log.debug("\t QueryString: " + request.getQueryString()); 255 log.debug("\t Request Params: "); 256 Enumeration e = request.getParameterNames(); 257 while (e.hasMoreElements()) { 258 String name = (String ) e.nextElement(); 259 log.debug("\t\t " + name + " = " 260 + request.getParameter(name)); 261 } 262 } 263 264 try { 265 boolean precompile = preCompile(request); 266 serviceJspFile(request, response, jspUri, null, precompile); 267 } catch (RuntimeException e) { 268 throw e; 269 } catch (ServletException e) { 270 throw e; 271 } catch (IOException e) { 272 throw e; 273 } catch (Throwable e) { 274 throw new ServletException (e); 275 } 276 277 } 278 279 public void destroy() { 280 if (log.isDebugEnabled()) { 281 log.debug("JspServlet.destroy()"); 282 } 283 284 rctxt.destroy(); 285 } 286 287 288 public void periodicEvent() { 289 rctxt.checkCompile(); 290 } 291 292 294 private void serviceJspFile(HttpServletRequest request, 295 HttpServletResponse response, String jspUri, 296 Throwable exception, boolean precompile) 297 throws ServletException , IOException { 298 299 JspServletWrapper wrapper = 300 (JspServletWrapper) rctxt.getWrapper(jspUri); 301 if (wrapper == null) { 302 synchronized(this) { 303 wrapper = (JspServletWrapper) rctxt.getWrapper(jspUri); 304 if (wrapper == null) { 305 if (null == context.getResource(jspUri)) { 308 response.sendError(HttpServletResponse.SC_NOT_FOUND, 309 jspUri); 310 return; 311 } 312 boolean isErrorPage = exception != null; 313 wrapper = new JspServletWrapper(config, options, jspUri, 314 isErrorPage, rctxt); 315 rctxt.addWrapper(jspUri,wrapper); 316 } 317 } 318 } 319 320 wrapper.service(request, response, precompile); 321 322 } 323 324 325 } 326 | Popular Tags |