Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use JSP/JSTL to generate dynamic css/javascript files?

If so how do you do this?

(jboss/tomact embedded/jdk 1.5)

not embedded js/css but an actual file...

like image 752
qodeninja Avatar asked Dec 01 '22 06:12

qodeninja


2 Answers

Sure you can. Only thing you need to do is to set the appropriate content type.

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

or

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

Take care with the fact that some webbrowsers might be picky on the file extension used in the actual request URL. I have never tried it as I normally would use a Servlet for those purposes, but I won't be surprised if especially MSIE won't eat that.

like image 158
BalusC Avatar answered Dec 21 '22 23:12

BalusC


What you want to do is assign the *.css servlet mapping to the JSPServlet.

In most containers, you will see a mapping like this (this is from Glassfish, in it's default-web.xml):

  <servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
      <param-name>xpoweredBy</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

Here, it is declaring the JSP servlet, and mapping "*.jsp" to it. So, in this case, the JSP servlet reference name is, simply 'jsp'.

So you would want to add:

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

When you do that, "suddenly" ALL of your CSS files are, effectively, JSPs, so you can do with them whatever you want.

The detail is I don't know if 'jsp' is the same for ALL containers, so your web.xml MAY NOT be portable.

But that's the gist of what you want to do. If you don't want ALL CSS to be JSPs, you could put the files in their own directory, and map that to the JSP servlet. Then ANYTHING you put in there would be a JSP (css, js, etc.)

like image 28
Will Hartung Avatar answered Dec 21 '22 23:12

Will Hartung