Sebastien Dionne

  • Increase font size
  • Default font size
  • Decrease font size

JSP On Grizzly

E-mail Print PDF

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

I'll show you how to to compile your JSP with Jasper and use them with GrizzlyWebServer.
I used Jasper that came with Tomcat 6. I'll need 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 framework like Struts, you will have more work to do, but I won't cover that.

<html>
	<body>
		
		Hello to you : 
		<table>
			<tr>
		<% for(int i=0;i<5;i++){%>
			<td><%=i%></td>
		<%}%>
		</tr>
	</table>
	</body>	
</html>

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();
		
		jasper.setArgs(params);
		jasper.execute();
}

...  

String jasperParams[] = new String[]{
				"-webapp", 
				"./jsp",
				"-v",
				"-p", 
				"ca.sebastiendionne", 
				"-d", 
				"./classes", 
				"-l", 
				"-s", 
				//"-webxml", // if you want JSPC to generate a webxml
				//"./web_jasper.xml", 
				"-compile"};

//compile the JSP
compileJSP(jasperParams);

That will generate the welcome_jsp.java 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();
            sa.setContextPath("/");
            
            // need to load JspRuntimeContext
            Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
            
            Servlet servlet = (Servlet)ClassLoaderUtil.load("ca.sebastiendionne.welcome_jsp");
    	      sa.setServletInstance(servlet);
            
    	      ws.addGrizzlyAdapter(sa, new String[]{});
            ws.start();
        } catch (Exception e){
            s_logger.error("Error Launching GrizzlyWebServer",e);
        }
}

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

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.

Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! Technorati! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Comments
Add New Search
wow power leveling  - fgfgfgfg 来祝贺     |123.145.187.xxx |2009-10-11 16:40:03
if wow gold and wow gold or wow power leveling
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Thursday, 29 January 2009 13:45 )