Jasper JSP Engine On Resin Deployment

From Resin 4.0 Wiki

Jump to: navigation, search

Cookbook-48.pngShare-48.pngWeb-48.png

Contents

Focus on: deploying Jasper JSP 2 Engine Resin server

Overview


Though we are planning on providing the integration library in release 4.0.31 or later, you can integrate Jasper into earlier versions of Resin using this reference. To get Jasper up and running you will need to download the referenced in prerequisites section software and copy / paste web.xml mapping along with two classes that complete the integration and a few Jasper dependencies residing in Tomcat.

Software Prerequisites


  • Resin (Pro) 4.0.29 or later (http://www.caucho.com/download)
  • Tomcat 7 for importing the required .jar files( located at http://tomcat.apache.org/download-70.cgi )

Copying the Jar Files

First copy the following jar files from Tomcat to your web app: exporting TOMCAT_HOME and MY_APP will help the copy/paste work on the following script.

cp $TOMCAT_HOME/lib/jasper.jar $MY_APP/WEB-INF/lib/
cp $TOMCAT_HOME/lib/tomcat-api.jar $MY_APP/WEB-INF/lib/
cp $TOMCAT_HOME/lib/tomcat-util.jar $MY_APP/WEB-INF/lib/
cp $TOMCAT_HOME/lib/servlet-api.jar $MY_APP/WEB-INF/lib/
cp $TOMCAT_HOME/lib/ecj-?.?.?.jar $MY_APP/WEB-INF/lib/
cp $TOMCAT_HOME/bin/tomcat-juli.jar $MY_APP/WEB-INF/lib/

Map Jasper Servlet

Create web.xml file and copy the mapping information in.
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
    <listener-class>jasper.ContextListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>jasper</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

    <init-param>
      <param-name>compiler</param-name>
      <param-value>internal</param-value>
    </init-param>

    <init-param>
      <param-name>fork</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>xpoweredBy</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jasper</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

</web-app>

Copy the ContextListener.java source into WEB-INF/classes/jasper/ContextListener.java

package jasper;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextListener implements ServletContextListener
{
  private InstanceManager _manager; 

  public void contextInitialized(ServletContextEvent event)
  {
    _manager = new InstanceManager();

    String key = org.apache.tomcat.InstanceManager.class.getName();

    event.getServletContext().setAttribute(key, _manager);
  }

  public void contextDestroyed(ServletContextEvent event)
  {
    _manager.close();
  }
}

Copy the InstanceManager.java source into WEB-INF/classes/jasper/InstanceManager.java

 package jasper;
 
 import com.caucho.config.inject.InjectManager;
 
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.InjectionTarget;
 import javax.naming.NamingException;
 import java.lang.reflect.InvocationTargetException;
 
 public class InstanceManager implements org.apache.tomcat.InstanceManager
 {
 
   public void destroyInstance(Object instance)
     throws IllegalAccessException, InvocationTargetException
   {
     BeanManager beanManager = InjectManager.create();
 
     Class instanceClass = instance.getClass();
 
     AnnotatedType annotatedType
       = beanManager.createAnnotatedType(instanceClass);
 
     InjectionTarget injectionTarget
       = beanManager.createInjectionTarget(annotatedType);
 
     injectionTarget.preDestroy(instance);
 
     injectionTarget.dispose(instance);
   }
 
   public void newInstance(final Object instance)
     throws IllegalAccessException, InvocationTargetException, NamingException
   {
     Class instanceClass = instance.getClass();
 
     newInstance(instance, instanceClass);
   }
 
   public Object newInstance(String className)
     throws IllegalAccessException, InvocationTargetException, NamingException,
     InstantiationException, ClassNotFoundException
   {
     ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 
     Class instanceClass = classLoader.loadClass(className);
 
     Object instance = newInstance(null, instanceClass);
 
     return instance;
   }
 
   public Object newInstance(String className, ClassLoader classLoader)
     throws IllegalAccessException, InvocationTargetException, NamingException,
     InstantiationException, ClassNotFoundException
   {
     Class instanceClass = classLoader.loadClass(className);
 
     Object instance = newInstance(null, instanceClass);
 
     return instance;
   }
 
   private Object newInstance(Object instance, Class instanceClass)
   {
     BeanManager beanManager = InjectManager.create();
 
     AnnotatedType annotatedType
       = beanManager.createAnnotatedType(instanceClass);
 
     InjectionTarget injectionTarget
       = beanManager.createInjectionTarget(annotatedType);
 
     CreationalContext context = beanManager.createCreationalContext(null);
 
     if (instance == null) {
       instance = injectionTarget.produce(context);
     }
 
     injectionTarget.inject(instance, context);
 
     injectionTarget.postConstruct(instance);
 
     return instance;
   }
 
   public void close()
   {
   }
 }

Test your integration

Create a sample JSP page.

<%
   Class tipClass = this.getClass();

   do {
      out.println(tipClass);
      tipClass = tipClass.getSuperclass();
   } while (! Object.class.equals(tipClass));
 %>


Issuing request to the p;age with curl or similar should produce the following output:

curl http://localhost:8080/jasper/test.jsp
 class org.apache.jsp.test_jsp
class org.apache.jasper.runtime.HttpJspBase
class javax.servlet.http.HttpServlet
class javax.servlet.GenericServlet
Personal tools
TOOLBOX
LANGUAGES