4

If so how do you do this?

(jboss/tomact embedded/jdk 1.5)

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

2

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

second glance this is what I was looking for
2

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.

Comments

1

On glassfish 3.1 you may need to add this:

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

And then

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

To your web.xml, if not, you may experience "java.lang.RuntimeException: There is no web component by the name of default here. " Error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.