5

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?

Chris Kuehl
  • 4,127
  • 3
  • 16
  • 19
  • Probably - the question is, what for? The only purpose of a file extension on a static file is to let the web server know what to put in the content-type header when that file is sent. If you're setting the content-type explicitly, why do you care about the file extension? – Mike Baranczak Mar 07 '11 at 05:02

4 Answers4

7

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.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • Is it possible to set this in the WAR package? The only file I have in NetBeans is `sun-web.xml`, and it doesn't work to add these there. The problem is that I won't have access to the machine I'm deploying on since I am just passing the WAR file off to someone who will deploy it. – Chris Kuehl Mar 07 '11 at 23:45
  • The web.xml is part of the WAR package (arguably it's what makes an arbitrary JAR file a "WAR"). Your WAR has a web.xml: /WEB-INF/web.xml. Open it up and stuff that servlet-mapping in there. Should work peachy. – Will Hartung Mar 08 '11 at 00:04
  • I've renamed the WAR file to .zip, but inside the WEB-INF folder are "lib" (folder), "classes" (folder), and sun-web.xml. There's no web.xml, and sun-web.xml seems to have a different format. – Chris Kuehl Mar 08 '11 at 00:28
  • Is a web app you are making from scratch? Are you simply using JSPs? If you don't have a web.xml, then you can just make one. I edited my post to include the proper headers. – Will Hartung Mar 08 '11 at 01:24
  • If you want to support the .css extensions *in addition* to .jsp you have to define the *.jsp mapping as well: ` jsp *.tag *.jsp ` – Udo May 28 '13 at 15:23
0

If you don't have too many CSS files to work with, you could add a servlet mapping for each CSS file which would redirect to a servlet and render the JSP.

Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
0

I don't know that what you are trying to accomplish is really necessary. You could just use scriptlets or jstl to dynamically append the context root when linking to your CSS,JS,images,etc.

You can see a discussion about that here:

Any clever ways of handling the context in a web app?

Community
  • 1
  • 1
Jordan Owens
  • 692
  • 4
  • 10
0

You could use a jsp include directive.

<%@ include file="something.css" %>

<%@ include file="something.xyz" %>
Sumit
  • 1,661
  • 1
  • 13
  • 18