Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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;
 
 import static org.jboss.web.JasperMessages.MESSAGES;
 
 
 
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(s):
Anil K. Vijendran
Harish Prabandham
Remy Maucherat
Kin-man Chung
Glenn Nielsen
 
 public class JspServlet extends HttpServlet implements PeriodicEventListener {
 
     private ServletContext context;
     private ServletConfig config;
     private Options options;
     private JspRuntimeContext rctxt;
 
 
     /*
      * Initializes this JspServlet.
      */
     public void init(ServletConfig configthrows ServletException {
         
         super.init(config);
         this. = config;
         this. = config.getServletContext();
         
         Object optionsObject = .getAttribute(.);
         if (optionsObject != null && (optionsObject instanceof Options)) {
              = (OptionsoptionsObject;
         } else {
             // Initialize the JSP Runtime Context
             // Check for a custom Options implementation
             String engineOptionsName = 
                 config.getInitParameter("engineOptionsClass");
             if (engineOptionsName != null) {
                 // Instantiate the indicated Options implementation
                 try {
                     ClassLoader loader = Thread.currentThread()
                     .getContextClassLoader();
                     Class engineOptionsClass = loader.loadClass(engineOptionsName);
                     Class[] ctorSig = { ServletConfig.classServletContext.class };
                     Constructor ctor = engineOptionsClass.getConstructor(ctorSig);
                     Object[] args = { config };
                      = (Optionsctor.newInstance(args);
                 } catch (Throwable e) {
                     ..failedLoadingOptions(engineOptionsNamee);
                     // Use the default Options implementation
                      = new EmbeddedServletOptions(config);
                 }
             } else {
                 // Use the default Options implementation
                  = new EmbeddedServletOptions(config);
             }
        }
         = new JspRuntimeContext();
    }


    
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.

This info may be used for monitoring purposes.

Returns:
The number of JSPs that have been loaded into the webapp with which this JspServlet is associated
    public int getJspCount() {
        return this..getJspCount();
    }


    
Resets the JSP reload counter.

Parameters:
count Value to which to reset the JSP reload counter
    public void setJspReloadCount(int count) {
        this..setJspReloadCount(count);
    }


    
Gets the number of JSPs that have been reloaded.

This info may be used for monitoring purposes.

Returns:
The number of JSPs (in the webapp with which this JspServlet is associated) that have been reloaded
    public int getJspReloadCount() {
        return this..getJspReloadCount();
    }


    

Look for a precompilation request as described in Section 8.4.2 of the JSP 1.2 Specification. WARNING - we cannot use request.getParameter() for this, because that will trigger parsing all of the request parameters, and not give a servlet the opportunity to call request.setCharacterEncoding() first.

Parameters:
request The servlet requset we are processing
Throws:
javax.servlet.ServletException if an invalid parameter value for the jsp_precompile parameter name is specified
    boolean preCompile(HttpServletRequest requestthrows ServletException {
        String queryString = request.getQueryString();
        if (queryString == null) {
            return (false);
        }
        int start = queryString.indexOf(.);
        if (start < 0) {
            return (false);
        }
        queryString =
            queryString.substring(start + ..length());
        if (queryString.length() == 0) {
            return (true);             // ?jsp_precompile
        }
        if (queryString.startsWith("&")) {
            return (true);             // ?jsp_precompile&foo=bar...
        }
        if (!queryString.startsWith("=")) {
            return (false);            // 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")) {
            return (true);             // ?jsp_precompile=true
        } else if (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.
            return (true);             // ?jsp_precompile=false
        } else {
            throw new ServletException(.invalidRequestParameterValue(.value));
        }
    }
    
    public void service (HttpServletRequest request
    			 HttpServletResponse response)
                throws ServletExceptionIOException {
        String jspUri = null;
        String jspFile = (Stringrequest.getAttribute(.);
        if (jspFile != null) {
            // JSP is specified via <jsp-file> in <servlet> declaration
            jspUri = jspFile;
        } else {
            /*
             * Check to see if the requested JSP has been the target of a
             * RequestDispatcher.include()
             */
            jspUri = (Stringrequest.getAttribute(.);
            if (jspUri != null) {
                /*
		 * Requested JSP has been target of
                 * RequestDispatcher.include(). Its path is assembled from the
                 * relevant javax.servlet.include.* request attributes
                 */
                String pathInfo = (Stringrequest.getAttribute(
                                    "javax.servlet.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;
                }
            }
        }
        try {
            boolean precompile = preCompile(request);
            serviceJspFile(requestresponsejspUrinullprecompile);
        } catch (RuntimeException e) {
            throw e;
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } catch (Throwable e) {
            throw new ServletException(e);
        }
    }
    public void destroy() {
        .destroy();
    }
    public void periodicEvent() {
        .checkCompile();
    }
    // -------------------------------------------------------- Private Methods
    private void serviceJspFile(HttpServletRequest request,
                                HttpServletResponse responseString jspUri,
                                Throwable exceptionboolean precompile)
        throws ServletExceptionIOException {
        JspServletWrapper wrapper =
            (JspServletWrapper.getWrapper(jspUri);
        if (wrapper == null) {
            synchronized(this) {
                wrapper = (JspServletWrapper.getWrapper(jspUri);
                if (wrapper == null) {
                    // Check if the requested JSP page exists, to avoid
                    // creating unnecessary directories and files.
                    if (null == .getResource(jspUri)) {
                        String includeRequestUri = (String)
                        request.getAttribute(
                                "javax.servlet.include.request_uri");
                        if (includeRequestUri != null) {
                            // This file was included. Throw an exception as
                            // a response.sendError() will be ignored
                            String msg = .fileNotFound(jspUri);
                            // Strictly, filtering this is an application
                            // responsibility but just in case...
                            throw new ServletException(SecurityUtil.filter(msg));
                        } else {
                            try {
                                response.sendError(
                                        .,
                                        request.getRequestURI());
                            } catch (IllegalStateException ise) {
                                ..fileNotFound(jspUri);
                            }
                        }
                        return;
                    }
                    boolean isErrorPage = exception != null;
                    wrapper = new JspServletWrapper(jspUri,
                                                    isErrorPage);
                    .addWrapper(jspUri,wrapper);
                }
            }
        }
        wrapper.service(requestresponseprecompile);
    }
New to GrepCode? Check out our FAQ X