How can I use JSP to create content pages with a non-JSP extension?

I am developing a web application for deployment on the latest Glassfish server.

To make the application compatible with different context roots (for example, "/ apps / myapp /"), I need CSS files to be created in it.

The problem is that these pages are not processed as JSP files, so I cannot use <%= contextRoot %> . I know that I could use a JSP file with a Content-Type header to simulate a CSS file, but I would prefer to have a CSS extension.

Is it possible for Glassfish to treat a non-JSP file as a JSP file?

+4
source share
4 answers

It's simple, I've done it before, it works great.

Just take the extension you want to map and map it to the JSP servlet.

JSPs are handled by the servlet, like everything else. Nothing special about them.

So, for Glassfish, this servlet is called "jsp". I don’t know if this is portable (for example, the name), but most likely it works in Glassfish and Tomcat, and probably anywhere where the JSP-JSP compiler is used.

In Glassfish, it is defined in $ glassfish_domain_dir / config / default-web.xml.

So add this to your web.xml

 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> </web-app> 

The good thing is that this will work pretty much for direct CSS files if they don't have markup, or with custom ones that you also add markup.

+6
source

If you don’t have too many CSS files to work with, you can add a servlet mapping for each CSS file that will be redirected to the servlet and display the JSP.

0
source

I do not know that what you are trying to accomplish is really necessary. You could just use scripts or jstl to dynamically add the context root when binding to your CSS, JS, images, etc.

You can see the discussion here:

Any smart ways to handle context in a web application?

0
source

You can use the jsp include directive.

 <%@ include file="something.css" %> <%@ include file="something.xyz" %> 
0
source

Source: https://habr.com/ru/post/1342577/


All Articles