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?
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.
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.
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?
You could use a jsp include directive.
<%@ include file="something.css" %>
<%@ include file="something.xyz" %>
精彩评论