Glassfish 4.1.1 and Standalone JSP Compiler (JSP and OSGI)

As everyone knows, jsp cannot work with classes outside of the current osgi web archive package. This is a bug in GF. The glassfish developers to work around this bug https://java.net/jira/browse/GLASSFISH-11208 suggest using a standalone jsp compiler (in other words, compile jsp files not during deployment, but during archive build). Ok, and I used the jspc-maven-plugin to compile my jsp while creating the wab.

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jspc-maven-plugin</artifactId>
 <version>1.4.6</version>
 <executions>
     <execution>
         <goals>
             <goal>compile</goal>
         </goals>
         <id>compile</id>
     </execution>
 </executions>
 <configuration>
 </configuration>
</plugin>

      

jsp are compiled and I can see their .classes in the built-in web archive.

Now the problem is how can I get the glassfish to use my compiled jsp, but not compile it myself? Because I see that GF ignores compiled .classes and generates .javas and compiles them on its own.

EDIT 1 What I am doing so far: 1) I added to glassfish-web.xml

  <jsp-config>
        <property name="usePrecompiled" value="true"/>
        <!-- to see it doesn't generate .javas -->
        <property name="keepgenerated" value="true" />
    </jsp-config>

      

2) And when I create my wab archive, I have jsp classes in WEB-INF/classes/jsp/...

However, I get an exception that the jsp file was not found. When I manually

move the jsp classes into WEB-INF/classes/org/apache/jsp...

I can see that the container now sees these classes, but I get

  StandardWrapperValve[default]: Servlet.service() for servlet default threw exception
java.lang.NoClassDefFoundError: org/apache/jsp/... (wrong name: jsp/...)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2370)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2154)
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1542)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1925)
    at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:978)
    at org.glassfish.osgijavaeebase.BundleClassLoader.loadClass(BundleClassLoader.java:79)
    at org.glassfish.osgiweb.OSGiWebDeploymentContext$WABClassLoader.loadClass(OSGiWebDeploymentContext.java:169)
    at org.glassfish.osgiweb.OSGiWebDeploymentContext$WABClassLoader.loadClass(OSGiWebDeploymentContext.java:154)
    at org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:654)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:202)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:626)

      

We know that this is the correct path - org / apache / jsp. The question is, how do I make a maven plugin to output in this direction?

EDIT 2 So I found the settings of this maven plugin -

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jspc-maven-plugin</artifactId>
 <version>1.4.6</version>
 <executions>
     <execution>
         <goals>
             <goal>compile</goal>
         </goals>
         <id>compile</id>
         <configuration>
             <packageName>org.apache.jsp</packageName>
         </configuration>
     </execution>
 </executions>
 <configuration>
 </configuration>
</plugin>

      

However, this is the end point, but not the result. As I am not getting exceptions but the returned HTTP request is empty (blank page in browser). It seems I should be using a different maven plugin, but which one?

0


source to share


1 answer


So, in addition to all the steps I took and explained in my edit, the web.xml file needs to be changed because the plugin will add the mapping there for the servlets generated in the jsp pages. So, the final settings:

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jspc-maven-plugin</artifactId>
 <version>1.4.6</version>
 <executions>
     <execution>
         <goals>
             <goal>compile</goal>
         </goals>
         <id>compile</id>
         <configuration>
             <!-- package where the compiled jsp classes will be put -->
             <packageName>org.apache.jsp</packageName>
             <!-- the plugin adds servlets to this web.xml file -->
<outputWebXml>${project.build.directory}/web.xml</outputWebXml>
          <verbose>true</verbose>
         <target>8</target>
         <source>8</source>
         </configuration>
     </execution>
 </executions>
 <configuration>
 </configuration>
</plugin>

      

EDIT Finally, I found out that the jasper version in GlassFish 4.1 is unknown or may even be changed. I have exceptions that no such method was found, etc. So I ended up with the following: I downloaded the sources of this plugin and used its glassfish version of jasper. I didn't make any changes to the plugin source code, only to the pom.xml. So, the final press was:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <!--<parent>
    <artifactId>mojo</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <version>11</version>
  </parent>-->
    <modelVersion>4.0.0</modelVersion>
      <groupId>org.codehaus.mojo</groupId>
    <artifactId>jspc-maven-plugin</artifactId>
    <version>1.4.6</version>
    <packaging>maven-plugin</packaging>
    <name>Maven Jspc plugin</name>
    <developers>
        <developer>
            <name>Jeff Genender</name>
            <email>jgenender@apache.org</email>
            <organization>Savoir Technologies</organization>
            <organizationUrl>http://www.savoirtech.com</organizationUrl>
            <timezone>-7</timezone>
        </developer>
    </developers>
    <contributors>
        <contributor>
            <name>Grzegorz Slowikowski</name>
            <email>gs@tiger.com.pl</email>
            <organization>Scott Tiger S.A.</organization>
            <organizationUrl>http://www.tiger.com.pl</organizationUrl>
            <timezone>+1</timezone>
        </contributor>
        <contributor>
            <name>Pawel Pastula</name>
            <email>pablo@tiger.com.pl</email>
            <organization>Scott Tiger S.A.</organization>
            <organizationUrl>http://www.tiger.com.pl</organizationUrl>
            <timezone>+1</timezone>
        </contributor>
    </contributors>

    <dependencies>
        <!-- from glassfish 4.1.1 modules folder we need:
        javax.servlet.jsp.jar
        javax.servlet-api.jar
        javax.servlet.jsp-api.jar
        javax.el.jar
        javax.servlet.jsp.jstl-api.jar
        javax.servlet.jsp.jstl.jar
        what versions of this jar you can find out in parent pom of glassfish
        http://repo.maven.apache.org/maven2/org/glassfish/main/glassfish-parent/4.1.1/glassfish-parent-4.1.1.pom
        and in manifest file
        -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.2-b01</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp</artifactId>
            <version>2.3.3-b02</version>
        </dependency>        
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp.jstl</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!-- we need this dependency as it contais tld files for core tag library -->
        <dependency>
            <groupId>org.eclipse.jetty.orbit</groupId>
            <artifactId>org.apache.jasper.glassfish</artifactId>
            <version>2.2.2.v201112011158</version>
        </dependency>
        <dependency>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.6.5</version>
        </dependency>        
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13</version>
            <scope>runtime</scope>
        </dependency>        
    </dependencies>
</project>

      

When you compile the package, you will have to add the following dependencies:



<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp</artifactId>
    <version>2.3.3-b02</version>
</dependency>       
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.4</version>
</dependency>

      

Also, you will need to import some glassfish bags to get it to work. So you can use pre-compiled jps files with glassfish, but before that you need to do some things. And as you can see, you are linking your code to GF.

Most importantly, you can work with classes from other osgi packages in jsp! For those who work with osgi in java-ee, this can be very important. After completing all these steps, I must conclude that GF IS NOT SUPPORTED TO BE USED WITH PRECOMPILED JPS FILES

despite the suggestions from the developers.

I hope that at least one will appreciate the whole solution, because it seems to me like this is the first description on the web of how to use pre-compiled jps pages with GF. By the way, if you are using osgi and complain, it cannot find classes importing required packages.

+1


source







All Articles