views:

437

answers:

4

If so how do you do this?

(jboss/tomact embedded/jdk 1.5)

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

+1  A: 

Sure, JSP can output any necessary text you need be that (X)HTML or CSS or JavaScript code. I do this regularly for ERP customization, inject a javascript script at the end of every page and via the context in which it loads able to manipulate necessary data fields on the page without touching the underlying app.

Xepoch
+6  A: 

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.

BalusC
hmm Servlet eh??
codeninja
@amvx: that's the next step after learning JSP :)
BalusC
+2  A: 

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.)

Will Hartung
excellent feedback!
codeninja
A: 

Any idea how to do this per webapp, ideally in the web.xml of the webapp?

I do not want to mess with the global config.

Yves
Welcome at stackoverflow! You may have more luck posting a question as a question instead of as an answer :) There's an `Ask Question` button at the right top. Feel free to include links to topics which you found in search but didn't help much (and elaborate *why* they didn't help).
BalusC