Is there a cross-platform way for accessing the JSP servlet?

In Tomcat, we can configure the Web app to treat non JSP files as JSP using the JSP servlet:

<servlet-mapping>  
   <servlet-name>jsp</servlet-name>  
   <url-pattern>/scripts/my.js</url-pattern>
</servlet-mapping>

Is there a cross-platform way to map resources to the JSP servlet?

UPDATE: While there’s not a cross-platform way available for mapping resources to the JSP servlet, it is possible to treat non-JSP files as JSP, using the <jsp-property-group> element. For example, in order to treat all .js files as JSP, we can add the following fragment to the web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.js</url-pattern>
        <is-xml>false</is-xml>
    </jsp-property-group>
</jsp-config>

Answer

This is nowhere specified in JSP specification. So there’s no crossplatform way to map resources on the JSP servlet like that.

Your best bet will be to have an actual .jsp file generating the desired JS content and map the /scripts/my.js on a <jsp-file> instead of <servlet-class> as follows:

<servlet>  
   <servlet-name>js</servlet-name>  
   <jsp-file>/scripts/my.jsp</jsp-file>
</servlet>
<servlet-mapping>  
   <servlet-name>js</servlet-name>  
   <url-pattern>/scripts/my.js</url-pattern>
</servlet-mapping>

Leave a Reply

Your email address will not be published. Required fields are marked *