Can I use JSP / JSTL to generate dynamic css / javascript files?

If so, how do you do it?

(jboss / tomact embedded / jdk 1.5)

not embedded js / css, but the actual file ...

+4
source share
4 answers

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

In most containers, you will see this mapping (this is from Glassfish, it has 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 he declares the JSP servlet and maps it to "* .jsp". So, in this case, the JSP servlet link name is just "jsp".

So you would like to add:

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

When you do this, โ€œsuddenlyโ€ ALL of your CSS files are essentially JSPs, so you can do whatever you want with them.

I don't know if "jsp" is the same for ALL containers, so your web.xml CANNOT be portable.

But that is the essence of what you want to do. If you do not want ALL CSS to be JSPs, you can put the files in your own directory and map them to the JSP servlet. Then ANYTHING you put into will be JSP (css, js, etc.).

+5
source

Sure. The only thing you need to do is set the appropriate content type.

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

or

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

Take care that some web browsers may be picky about the file extension used in the actual request URL. I have never tried since I used Servlet for these purposes, but I wonโ€™t be surprised if especially MSIE will not eat it.

+10
source

Of course, JSP can print any text you need to (X) HTML or CSS or JavaScript code. I do this regularly to configure ERP, insert a javascript script at the end of each page and through the context in which it loads, is able to manipulate the necessary data fields on the page without touching the main application.

+2
source

In Glassfish 3.1, you may need to add the following:

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

In your web.xml, if not, you can experience "java.lang.RuntimeException: there is no web component by default here." Error

+1
source

All Articles