/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.jasper.servlet;
/** * The JSP engine (a.k.a Jasper). * * The servlet container is responsible for providing a * URLClassLoader for the web application context Jasper * is being used in. Jasper will try get the Tomcat * ServletContext attribute for its ServletContext class * loader, if that fails, it uses the parent class loader. * In either case, it must be a URLClassLoader. * * @author Anil K. Vijendran * @author Harish Prabandham * @author Remy Maucherat * @author Kin-man Chung * @author Glenn Nielsen
*/ publicclass JspServlet extends HttpServlet implements PeriodicEventListener {
privatetransient ServletContext context; private ServletConfig config; privatetransient Options options; privatetransient JspRuntimeContext rctxt; // jspFile for a jsp configured explicitly as a servlet, in environments where this // configuration is translated into an init-param for this servlet. private String jspFile;
// Initialize the JSP Runtime Context // Check for a custom Options implementation
String engineOptionsName = config.getInitParameter("engineOptionsClass"); if (Constants.IS_SECURITY_ENABLED && engineOptionsName != null) {
log.info(Localizer.getMessage( "jsp.info.ignoreSetting", "engineOptionsClass", engineOptionsName));
engineOptionsName = null;
} if (engineOptionsName != null) { // Instantiate the indicated Options implementation try {
ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class<?> engineOptionsClass = loader.loadClass(engineOptionsName); Class<?>[] ctorSig = { ServletConfig.class, ServletContext.class };
Constructor<?> ctor = engineOptionsClass.getConstructor(ctorSig);
Object[] args = { config, context };
options = (Options) ctor.newInstance(args);
} catch (Throwable e) {
e = ExceptionUtils.unwrapInvocationTargetException(e);
ExceptionUtils.handleThrowable(e); // Need to localize this.
log.warn(Localizer.getMessage("jsp.warning.engineOptionsClass", engineOptionsName), e); // Use the default Options implementation
options = new EmbeddedServletOptions(config, context);
}
} else { // Use the default Options implementation
options = new EmbeddedServletOptions(config, context);
}
rctxt = new JspRuntimeContext(context, options); if (config.getInitParameter("jspFile") != null) {
jspFile = config.getInitParameter("jspFile"); try { if (null == context.getResource(jspFile)) { return;
}
} catch (MalformedURLException e) { thrownew ServletException(Localizer.getMessage("jsp.error.no.jsp", jspFile), e);
} try { if (SecurityUtil.isPackageProtectionEnabled()){
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
serviceJspFile(null, null, jspFile, true); returnnull;
});
} else {
serviceJspFile(null, null, jspFile, true);
}
} catch (IOException e) { thrownew ServletException(Localizer.getMessage("jsp.error.precompilation", jspFile), e);
} catch (PrivilegedActionException e) {
Throwable t = e.getCause(); if (t instanceof ServletException) { throw (ServletException)t;
} thrownew ServletException(Localizer.getMessage("jsp.error.precompilation", jspFile), e);
}
}
if (log.isDebugEnabled()) {
log.debug(Localizer.getMessage("jsp.message.scratch.dir.is",
options.getScratchDir().toString()));
log.debug(Localizer.getMessage("jsp.message.dont.modify.servlets"));
}
}
/** * Returns the number of JSPs for which JspServletWrappers exist, i.e., * the number of JSPs that have been loaded into the webapp with which * this JspServlet is associated. * * <p>This info may be used for monitoring purposes. * * @return The number of JSPs that have been loaded into the webapp with * which this JspServlet is associated
*/ publicint getJspCount() { returnthis.rctxt.getJspCount();
}
/** * Resets the JSP reload counter. * * @param count Value to which to reset the JSP reload counter
*/ publicvoid setJspReloadCount(int count) { this.rctxt.setJspReloadCount(count);
}
/** * Gets the number of JSPs that have been reloaded. * * <p>This info may be used for monitoring purposes. * * @return The number of JSPs (in the webapp with which this JspServlet is * associated) that have been reloaded
*/ publicint getJspReloadCount() { returnthis.rctxt.getJspReloadCount();
}
/** * Gets the number of JSPs that are in the JSP limiter queue * * <p>This info may be used for monitoring purposes. * * @return The number of JSPs (in the webapp with which this JspServlet is * associated) that are in the JSP limiter queue
*/ publicint getJspQueueLength() { returnthis.rctxt.getJspQueueLength();
}
/** * Gets the number of JSPs that have been unloaded. * * <p>This info may be used for monitoring purposes. * * @return The number of JSPs (in the webapp with which this JspServlet is * associated) that have been unloaded
*/ publicint getJspUnloadCount() { returnthis.rctxt.getJspUnloadCount();
}
/** * <p>Look for a <em>precompilation request</em> as described in * Section 8.4.2 of the JSP 1.2 Specification. <strong>WARNING</strong> - * we cannot use <code>request.getParameter()</code> for this, because * that will trigger parsing all of the request parameters, and not give * a servlet the opportunity to call * <code>request.setCharacterEncoding()</code> first.</p> * * @param request The servlet request we are processing * * @exception ServletException if an invalid parameter value for the * <code>jsp_precompile</code> parameter name is specified
*/ boolean preCompile(HttpServletRequest request) throws ServletException {
String precompileParameter = rctxt.getOptions().getJspPrecompilationQueryParameter();
String queryString = request.getQueryString(); if (queryString == null) { returnfalse;
} int start = queryString.indexOf(precompileParameter); if (start < 0) { returnfalse;
}
queryString =
queryString.substring(start + precompileParameter.length()); if (queryString.length() == 0) { returntrue; // ?jsp_precompile
} if (queryString.startsWith("&")) { returntrue; // ?jsp_precompile&foo=bar...
} if (!queryString.startsWith("=")) { returnfalse; // part of some other name or value
} int limit = queryString.length(); int ampersand = queryString.indexOf('&'); if (ampersand > 0) {
limit = ampersand;
}
String value = queryString.substring(1, limit); if (value.equals("true")) { returntrue; // ?jsp_precompile=true
} elseif (value.equals("false")) { // Spec says if jsp_precompile=false, the request should not // be delivered to the JSP page; the easiest way to implement // this is to set the flag to true, and precompile the page anyway. // This still conforms to the spec, since it says the // precompilation request can be ignored. returntrue; // ?jsp_precompile=false
} else { thrownew ServletException(Localizer.getMessage("jsp.error.precompilation.parameter",
precompileParameter, value));
}
// jspFile may be configured as an init-param for this servlet instance
String jspUri = jspFile;
if (jspUri == null) { /* * Check to see if the requested JSP has been the target of a * RequestDispatcher.include()
*/
jspUri = (String) request.getAttribute(
RequestDispatcher.INCLUDE_SERVLET_PATH); if (jspUri != null) { /* * Requested JSP has been target of * RequestDispatcher.include(). Its path is assembled from the * relevant jakarta.servlet.include.* request attributes
*/
String pathInfo = (String) request.getAttribute(
RequestDispatcher.INCLUDE_PATH_INFO); if (pathInfo != null) {
jspUri += pathInfo;
}
} else { /* * Requested JSP has not been the target of a * RequestDispatcher.include(). Reconstruct its path from the * request's getServletPath() and getPathInfo()
*/
jspUri = request.getServletPath();
String pathInfo = request.getPathInfo(); if (pathInfo != null) {
jspUri += pathInfo;
}
}
}
String msg = Localizer.getMessage("jsp.error.file.not.found",jspUri); if (includeRequestUri != null) { // This file was included. Throw an exception as // a response.sendError() will be ignored // Strictly, filtering this is an application // responsibility but just in case... thrownew ServletException(Escape.htmlElementContent(msg));
} else { try {
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
} catch (IllegalStateException ise) {
log.error(msg);
}
}
}
}
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.