Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use JSP to generate content pages with a non-JSP extension?

I'm developing a web application to be deployed on the latest Glassfish server.

To make the application compatible with different context roots (like "/apps/myapp/"), I need the CSS files in it to be generated dynamically.

The problem is that the these pages aren't treated like JSP files so I can't use <%= contextRoot %>. I know I could use a JSP file with a Content-Type header to mimic a CSS file, but I would prefer to have a CSS extension on it.

Is it possible to have Glassfish treat a non-JSP file as a JSP file?

like image 687
Chris Kuehl Avatar asked Mar 07 '11 04:03

Chris Kuehl


1 Answers

This is simple, I've done it before, works great.

Simply take the extension you want to map, and map it to the JSP servlet.

JSPs are processed by a servlet, like anything else. Nothing really special about them.

So, for Glassfish, this servlet happens to be named "jsp". I don't know whether that is portable (i.e. the name), but it likely runs in Glassfish and Tomcat, and probably anyplace that uses the Jasper JSP compiler.

In Glassfish, it's defined in $glassfish_domain_dir/config/default-web.xml.

So, add this to your web.xml

<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">    
  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
</web-app>

The nice thing that this will pretty much work for straight up CSS files if there's no markup in them, or with custom ones that you add markup too.

like image 92
Will Hartung Avatar answered Oct 30 '22 16:10

Will Hartung