Version: 9.4.12.v20180830 |

Chapter 8. Configuring JSP Support
Previous | Part II. Jetty Configuration Guide Home | Next

##### Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development

## Chapter 8. Configuring JSP Support

## Configuring JSP

This document provides information about configuring Java Server Pages (JSP) for Jetty.

### Which JSP Implementation

Jetty uses Jasper from Apache as the default JSP container implementation.

By default the Jetty distribution enables the JSP module, and by default, this module is set to Apache Jasper.

    DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html

    [description]
    Enables JSP for all webapplications deployed on the server.

    [depend]
    servlet
    annotations
    apache-jsp

Note that the availability of some JSP features may depend on which JSP container implementation you are using. Note also that it may not be possible to precompile your JSPs with one container and deploy to the other.

#### JSPs and Embedding

If you have an embedded setup for your webapp and wish to use JSPs, you will need to ensure that a JSP engine is correctly initialized.

For Apache, a Servlet Specification 3.1 style ServletContainerInitializer is used to accomplish this. You will need to ensure that this ServletContainerInitializer is run by jetty. Perhaps the easiest way to do this is to enable annotations processing so that Jetty automatically discovers and runs it. The Embedded Examples section includes a worked code example of how to do this.

Alternatively, you can manually wire in the appropriate ServletContainerInitializer as shown in the embedded-jetty-jsp example on GitHub, in which case you will not need the jetty-annotations jar on your classpath, nor include the AnnotationConfiguration in the list of configuration classes.

### Precompiling JSPs

You can either follow the instructions on precompilation provided by Apache, or if you are using Maven for your builds, you can use the jetty-jspc-maven plugin to do it for you.

If you have precompiled your JSPs, and have customized the output package prefix (which is `org.apache.jsp` by default), you should configure your webapp context to tell Jetty about this custom package name. You can do this using a servlet context init-param called `org.eclipse.jetty.servlet.jspPackagePrefix`.

For example, suppose you have precompiled your JSPs with the custom package prefix of `com.acme`, then you would add the following lines to your `web.xml` file:

      <context-param>
        <param-name>org.eclipse.jetty.servlet.jspPackagePrefix</param-name>
        <param-value>com.acme</param-value>
      </context-param>

> ### Note

> Both Jetty Maven plugins - jetty-jspc-maven-plugin and the jetty-maven-plugin - will only use Apache Jasper.

#### Apache JSP Container

By default, the Apache JSP container will look for the Eclipse Java Compiler (jdt). The Jetty distribution ships a copy of this in `{$jetty.home}/lib/apache-jsp`. If you wish to use a different compiler, you will need to configure the `compilerClassName` init-param on the `JspServlet` with the name of the class.

Table 8.1. Understanding Apache JspServlet Parameters

init param | Description | Default | `webdefault.xml`
--- | --- | --- | ---
classpath | `Classpath used for jsp compilation. Only used if org.apache.catalina.jsp_classpath context attribute is not set, which it is in Jetty.` | - | –
classdebuginfo | Include debugging info in class file. | TRUE | –
checkInterval | Interval in seconds between background recompile checks. Only relevant if ` development=false`. | 0 | –
development | `development=true`, recompilation checks occur on each request. See also ` modificationTestInterval`. | TRUE | –
displaySourceFragment | Should a source fragment be included in exception messages | TRUE | –
errorOnUseBeanInvalidClassAttribute | Should Jasper issue an error when the value of the class attribute in an useBean action is not a valid bean class | TRUE | –
fork | Should Ant fork its Java compiles of JSP pages? | TRUE | FALSE
keepgenerated | Do you want to keep the generated Java files around? | TRUE | –
trimSpaces | Should white spaces between directives or actions be trimmed? | FALSE | –
enablePooling | Determines whether tag handler pooling is enabled. | TRUE | –
engineOptionsClass | Allows specifying the Options class used to configure Jasper. If not present, the default EmbeddedServletOptions will be used. | – | mappedFile
Support for mapped Files. Generates a servlet that has a print statement per line of the JSP file | TRUE | – | suppressSmap
Generation of SMAP info for JSR45 debugging. | FALSE | – | dumpSmap
Dump SMAP JSR45 info to a file. | FALSE | – | genStrAsCharArray
Option for generating Strings. | FALSE | – | ieClassId
The class-id value to be sent to Internet Explorer when using <jsp:plugin> tags. | clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 | – | maxLoadedJsps
The maximum number of JSPs that will be loaded for a web application. If more than this number of JSPs are loaded, the least recently used JSPs will be unloaded so that the number of JSPs loaded at any one time does not exceed this limit. A value of zero or less indicates no limit. | -1 | – | jspIdleTimeout
The amount of time in seconds a JSP can be idle before it is unloaded. A value of zero or less indicates never unload. | -1 | – | scratchDir
Directory where servlets are generated. See | – | – | compilerClassName
If not set, defaults to the Eclipse jdt compiler. | – | compiler | Used if the Eclipse jdt compiler cannot be found on the classpath. It is the classname of a compiler that Ant should invoke.
– | – | compilerTargetVM | Target vm to compile for.
1.7 | – | compilerSourceVM | Sets source compliance level for the jdt compiler.
1.7 | – | javaEncoding | Pass through the encoding to use for the compilation.
UTF8 | – | modificationTestInterval | If `development=true`, interval between recompilation checks, triggered by a request.
4 | – | xpoweredBy | Generate an X-Powered-By response header.
FALSE | FALSE | recompileOnFail | If a JSP compilation fails should the modificationTestInterval be ignored and the next access trigger a re-compilation attempt? Used in development mode only and is disabled by default as compilation may be expensive and could lead to excessive resource usage.

#### Configuration

The JSP engine has many configuration parameters. Some parameters affect only precompilation, and some affect runtime recompilation checking. Parameters also differ among the various versions of the JSP engine. This page lists the configuration parameters, their meanings, and their default settings. Set all parameters on the `org.apache.jasper.JspServlet` instance defined in the `webdefault.xml` file.

> ### Note

> Be careful: for all of these parameters, if the value you set doesn’t take effect, try using all lower case instead of camel case, or capitalizing only some of the words in the name, as JSP is inconsistent in its parameter naming strategy.

### Modifying Configuration

#### Overriding `webdefault.xml`

You can make a copy of the {$jetty.home}/etc/webdefault.xml that ships with Jetty, apply your changes, and use it instead of the shipped version. The example below shows how to do this when using the Jetty Maven plugin.

      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
          <webApp>
            <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
          </webApp>
      </plugin>

If you are using the Jetty distribution, and you want to change the JSP settings for just one or a few of your webapps, copy the `{$jetty.home}/etc/webdefault.xml` file somewhere, modify it, and then use a context xml file to set this file as the `defaultsDescriptor` for your webapp. Here’s a snippet:

     <Configure class=>"org.eclipse.jetty.webapp.WebAppContext">

       <Set name=>"contextPath">/foo</Set>
       <Set name=>"war"><SystemProperty name=>"jetty.home" >default=>"."/>/webapps/foobar.war</Set>
       <Set name=>"defaultsDescriptor">/home/smith/dev/webdefault.xml</Set>

     </Configure>

If you want to change the JSP settings for all webapps, edit the `{$jetty.home}/etc/webdefaults.xml` file directly instead.

#### Configuring the JSP Servlet in web.xml

Another option is to add an entry for the JSPServlet to the `WEB-INF/web.xml` file of your webapp and change or add init-params. You may also add (but not remove) servlet-mappings. You can use the entry in {$jetty.home}/etc/webdefault.xml as a starting point.

     <servlet id="jsp">
         <servlet-name>jsp</servlet-name>
         <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
         <init-param>
             <param-name>logVerbosityLevel</param-name>
             <param-value>DEBUG</param-value>
         </init-param>
         <init-param>
             <param-name>fork</param-name>
             <param-value>>false</param-value>
         </init-param>
         <init-param>
             <param-name>keepgenerated</param-name>
             <param-value>>true</param-value>
         </init-param>
         ...

         <load-on-startup>0</load-on-startup>
       </servlet>

       <servlet-mapping>
         <servlet-name>jsp</servlet-name>
         <url-pattern>*.jsp</url-pattern>
         <url-pattern>*.jspf</url-pattern>
         <url-pattern>*.jspx</url-pattern>
         <url-pattern>*.xsp</url-pattern>
         <url-pattern>*.JSP</url-pattern>
         <url-pattern>*.JSPF</url-pattern>
         <url-pattern>*.JSPX</url-pattern>
         <url-pattern>*.XSP</url-pattern>
       </servlet-mapping>

       <servlet id="my-servlet">
         <servlet-name>myServlet</servlet-name>
         <servlet-class>com.acme.servlet.MyServlet</servlet-class>
          ...

#### Configuring Async Support

By default, Jetty does not enable async support for the JSP servlet. Configuring the JSP servlet for async is relatively easy - simply define the `async-supported` parameter as `true` in either your `webdefault.xml` or the `web.xml` for a specific context.

    <servlet id="jsp">
      <servlet-name>jsp</servlet-name>
      <async-supported>true</async-supported>
    </servlet>

### Using JSTL Taglibs

The JavaServer Pages Standlard Tag Library (JSTL) is part of the Jetty distribution and is automatically put on the classpath when you select your flavour of JSP. It is also automatically on the classpath for the Jetty Maven plugin, which uses the Apache JSP engine.

#### Embedding

If you are using Jetty in an embedded scenario, and you need to use JSTL, then you must ensure that the JSTL jars are included on the container’s classpath - that is the classpath that is the parent of the webapp’s classpath. This is a restriction that arises from the JavaEE specification.

##### Apache JSP

You will need to put the jars that are present in the `{$jetty.home}/lib/apache-jstl` directory onto the container’s classpath. The Apache JSP engine will find the JSTL tag definitions inside these jars during startup.

As an efficiency enhancement, you can have jetty examine the JSTL jars to find the tags, and pre-feed them into the Apache JSP engine. This is more efficient, because jetty will only scan the jars you tell it to, whereas the Apache JSP engine will scan every jar, which can be time-consuming in applications with a lot of jars on the container classpath.

To take advantage of this efficiency enhancement, set up the org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern to include a pattern that will match the names of the JSTL jars. The Embedded Examples section includes a worked code example of how to do this. Below is a snippet from the example:

      webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*taglibs.*\\.jar$");

### Using JSF Taglibs

The following sections provide information about using JSF TagLibs with Jetty Standalone and the Jetty Maven Plugin.

#### Using JSF Taglibs with Jetty Distribution

If you want to use JSF with your webapp, you need to copy the JSF implementation Jar (whichever Jar contains the `META-INF/*.tld` files from your chosen JSF implementation) into Jetty’s shared container lib directory. You can either put them into the lib directory for Apache `{$jetty.home}/lib/apache-jsp` or put them into `{$jetty.home}/lib/ext`.

#### Using JSF Taglibs with Jetty Maven Plugin

You should make your JSF jars dependencies of the plugin and not the webapp itself. For example:

       <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
           <webApp>
             <contextPath>/${artifactId}</contextPath>
           </webApp>
           <scanIntervalSeconds>5</scanIntervalSeconds>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0.8</version>
          </dependency>
          <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.8</version>
         </dependency>
        </dependencies>
      </plugin>

Previous | Top | Next
Spnego Support | Home | Part III. Jetty Administration Guide

See an error or something missing? Contribute to this documentation at Github!(Generated: 2018-08-30)