tags:

views:

37

answers:

2

I want to have a css file which is in fact a jsp-page. One of the reasons is that I would like to use c:url tags to make the path to images context-independent.

So far I only found a possibility to set this up in server.xml. But I need it for my webapp only, and not server-wide.


Update: Setting the content-type to text/css of course works. But it still leaves me with a style.jsp whereas I want the file to be named style.css. One of the reasons would be that Eclipse's syntax-highlighting and autocompletion do then work.

+1  A: 

Have you tried using a jsp extension on the file and its reference. You will have to change th mime type in the jsp.

BillThor
+1  A: 

The file extension is in fact irrelevant. The key is the HTTP Content-Type header. The browser uses this information to treat the response in an appropriate manner.

Put this in top of your to-be-CSS JSP file:

<%@ page contentType="text/css" %>

That's it.

If you leave the manual setting of the HTTP Content-Type header away, then the job of setting the Content-Type header will be taken over by the servletcontainer/webserver. This part is then sniffing the file extension to set the appropriate header.


Update: as per your update, you want to use the .css extension anyway to take benefit of IDE's highlighting and autocompletion. Then there's another way: map the CSS file on the servlet-name of the JspServlet as it is definied in the particular servletcontainer. In case of Tomcat, it's usually jsp.

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/style.css</url-pattern>
</servlet-mapping>

You should however take into account that the behaviour of your webapp will now be dependent of the servletcontainer in question. There may exist servletcontainers which doesn't use jsp as servlet-name.

BalusC