Skip to main content

JSP on GrizzlyWebServer

Posted by survivant on January 29, 2009 at 10:49 AM PST

We had a lot of people asking how to put JSP over Grizzly, but we didn't have an implementation or a working sample to show them.
It couldn't stay like that forever ... so I took a little of my spare time and I got something for you :)

I'll show you how to compile your JSP with Jasper and use them with GrizzlyWebServer.
I use Jasper that came with Tomcat 6. You will have to include some librairies to your project.
I didn't try with Tomcat 4.x, but maybe you could save some space.

I'll skip the jars required for Jasper to the end of this article. Now let's take a look at the beast (ok, mini-beast).

The JSP page is really simple. If you want to use a framework like Struts, you will have more work to do, but I won't cover that.

		Hello to you : 
		<% for(int i=0;i<5;i++){%>

We need to compile the JSP if we want to use it with our GrizzlyWebServer. It isn't really complicated.

public void compileJSP(String[] params) throws Exception {
		JspC jasper = new JspC();


String jasperParams[] = new String[]{
				//"-webxml", // if you want JSPC to generate a webxml

//compile the JSP

This will generate the and compile into /classes. Now we need to call it in our application.

public void launch(){
		GrizzlyWebServer ws = new GrizzlyWebServer(80);
		try {
            ServletAdapter sa = new ServletAdapter();
            // need to load JspRuntimeContext
            Servlet servlet = (Servlet)ClassLoaderUtil.load("ca.sebastiendionne.welcome_jsp");
    	      ws.addGrizzlyAdapter(sa, new String[]{});
        } catch (Exception e){
            s_logger.error("Error Launching GrizzlyWebServer",e);

The final touch is to test it. You will obtain a very nice output :)

Hello to you :
0 	1 	2 	3 	4

I didn't go to deep into JSPC. I used JSPC, the class used by the Ant task. So because of that, we will need
to include Ant jars into the project. If you have problems with that, I'm sure that you could extract the code from
the ant task too.
The jars needed are :

ant-launcher.jar // from ant 1.7x
ant.jar // from ant 1.7x
el-api.jar // from Tomcat 6.x
jasper-el.jar // from Tomcat 6.x
jasper-jdt.jar // from Tomcat 6.x
jasper.jar // from Tomcat 6.x
jsp-api.jar // from Tomcat 6.x
servlet-api.jar // from Tomcat 6.x
tomcat-juli.jar // from Tomcat 6.x : for the logging into JSPC

You can download the source code here.

Related Topics >>


Thank you for posting this. This is extremely useful.