use of jakarta.servlet.ServletException in project tomcat by apache.
the class JspServletWrapper method getServlet.
public Servlet getServlet() throws ServletException {
/*
* DCL on 'reload' requires that 'reload' be volatile
* (this also forces a read memory barrier, ensuring the new servlet
* object is read consistently).
*
* When running in non development mode with a checkInterval it is
* possible (see BZ 62603) for a race condition to cause failures
* if a Servlet or tag is reloaded while a compile check is running
*/
if (getReloadInternal() || theServlet == null) {
synchronized (this) {
// of different pages, but not the same page.
if (getReloadInternal() || theServlet == null) {
// This is to maintain the original protocol.
destroy();
final Servlet servlet;
try {
InstanceManager instanceManager = InstanceManagerFactory.getInstanceManager(config);
servlet = (Servlet) instanceManager.newInstance(ctxt.getFQCN(), ctxt.getJspLoader());
} catch (Exception e) {
Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
ExceptionUtils.handleThrowable(t);
throw new JasperException(t);
}
servlet.init(config);
if (theServlet != null) {
ctxt.getRuntimeContext().incrementJspReloadCount();
}
theServlet = servlet;
reload = false;
// Volatile 'reload' forces in order write of 'theServlet' and new servlet object
}
}
}
return theServlet;
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class JspServletWrapper method handleJspException.
/**
* <p>Attempts to construct a JasperException that contains helpful information
* about what went wrong. Uses the JSP compiler system to translate the line
* number in the generated servlet that originated the exception to a line
* number in the JSP. Then constructs an exception containing that
* information, and a snippet of the JSP to help debugging.
* Please see https://bz.apache.org/bugzilla/show_bug.cgi?id=37062 and
* http://www.tfenne.com/jasper/ for more details.
*</p>
*
* @param ex the exception that was the cause of the problem.
* @return a JasperException with more detailed information
*/
protected JasperException handleJspException(Exception ex) {
try {
Throwable realException = ex;
if (ex instanceof ServletException) {
realException = ((ServletException) ex).getRootCause();
}
// Find the first stack frame that represents code generated by
// Jasper
StackTraceElement[] frames = realException.getStackTrace();
StackTraceElement jspFrame = null;
String servletPackageName = ctxt.getBasePackageName();
for (StackTraceElement frame : frames) {
if (frame.getClassName().startsWith(servletPackageName)) {
jspFrame = frame;
break;
}
}
SmapStratum smap = null;
if (jspFrame != null) {
smap = ctxt.getCompiler().getSmap(jspFrame.getClassName());
}
if (smap == null) {
// smap to hand, we can't really add anything
return new JasperException(ex);
}
@SuppressWarnings("null") int javaLineNumber = jspFrame.getLineNumber();
SmapInput source = smap.getInputLineNumber(javaLineNumber);
// where in the JSP things went wrong
if (source.getLineNumber() < 1) {
throw new JasperException(ex);
}
JavacErrorDetail detail = new JavacErrorDetail(jspFrame.getMethodName(), javaLineNumber, source.getFileName(), source.getLineNumber(), null, ctxt);
if (options.getDisplaySourceFragment()) {
return new JasperException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + source.getLineNumber()) + System.lineSeparator() + System.lineSeparator() + detail.getJspExtract() + System.lineSeparator() + System.lineSeparator() + "Stacktrace:", ex);
}
return new JasperException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + source.getLineNumber()), ex);
} catch (Exception je) {
// If anything goes wrong, just revert to the original behaviour
if (ex instanceof JasperException) {
return (JasperException) ex;
}
return new JasperException(ex);
}
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class JspServlet method service.
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 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;
}
}
}
if (log.isDebugEnabled()) {
log.debug("JspEngine --> " + jspUri);
log.debug("\t ServletPath: " + request.getServletPath());
log.debug("\t PathInfo: " + request.getPathInfo());
log.debug("\t RealPath: " + context.getRealPath(jspUri));
log.debug("\t RequestURI: " + request.getRequestURI());
log.debug("\t QueryString: " + request.getQueryString());
}
try {
boolean precompile = preCompile(request);
serviceJspFile(request, response, jspUri, precompile);
} catch (RuntimeException | IOException | ServletException e) {
throw e;
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
throw new ServletException(e);
}
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class JspServlet method init.
/*
* Initializes this JspServlet.
*/
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
this.context = config.getServletContext();
// 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) {
throw new ServletException(Localizer.getMessage("jsp.error.no.jsp", jspFile), e);
}
try {
if (SecurityUtil.isPackageProtectionEnabled()) {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
serviceJspFile(null, null, jspFile, true);
return null;
});
} else {
serviceJspFile(null, null, jspFile, true);
}
} catch (IOException e) {
throw new ServletException(Localizer.getMessage("jsp.error.precompilation", jspFile), e);
} catch (PrivilegedActionException e) {
Throwable t = e.getCause();
if (t instanceof ServletException) {
throw (ServletException) t;
}
throw new 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"));
}
}
use of jakarta.servlet.ServletException in project tomcat by apache.
the class JspServlet method preCompile.
/**
* <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) {
return false;
}
int start = queryString.indexOf(precompileParameter);
if (start < 0) {
return false;
}
queryString = queryString.substring(start + precompileParameter.length());
if (queryString.length() == 0) {
// ?jsp_precompile
return true;
}
if (queryString.startsWith("&")) {
// ?jsp_precompile&foo=bar...
return true;
}
if (!queryString.startsWith("=")) {
// part of some other name or value
return false;
}
int limit = queryString.length();
int ampersand = queryString.indexOf('&');
if (ampersand > 0) {
limit = ampersand;
}
String value = queryString.substring(1, limit);
if (value.equals("true")) {
// ?jsp_precompile=true
return true;
} else if (value.equals("false")) {
// ?jsp_precompile=false
return true;
} else {
throw new ServletException(Localizer.getMessage("jsp.error.precompilation.parameter", precompileParameter, value));
}
}
Aggregations