tags:

views:

56

answers:

4

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

Aren't servlets platform dependent by definition (ie, they require the Java platform, and often the servlet platform).

Those platforms in turn, however, are often easy to configure to run on a different operating system, but the details aren't necessarily known by the servlet itself.

So... In answer to your question... Probably "No", unless you constrain your answer to, eg, tomcat specific on multiple operating systems, or particular versions of tomcat and a particular alternative servlet server.

Arafangion
With "platform" the OP actually meant "servlet container", not "operating system" as you seem to think :)
BalusC
Perhaps, but terminology matters, especially when people don't always realise that Java /is/ a platform, and not everyone here should need to know the definitions that have been reinvented for Java, we have enough overloaded terms as it is!
Arafangion
A: 

Hi -

I agree with Arafangion's first statement: servlets ARE cross-platform to begin with!

A servlet is cross platform, a JSP (which is an example of a servlet) is cross platform, and the web.xml snippet you gave is cross platform (it'll run on Windows the same as it'll run on Linux; you can use it in Tomcat, in JBoss or in Websphere, etc etc).

HOWEVER ...

You know, I'm sure, that "Java" != "Javascript". You also know that a servlet is server-side, but a .js script is processed client-side.

So I'm a bit confused about why you seem to be assigning a (server-side) "" to what appears to be a (client side) .js script?

+2  A: 

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>
BalusC
I just had a look at `JSP 2.1` specification and it seemed to me that `<jsp-property-group>` can be used to treat non-JSP files as if they are JSP. I tested it and it works. I will update my post to reflect this.
Bytecode Ninja
Oh nice one, didn't thought of that immediately.
BalusC
+1  A: 

Probably easiest to just call it .jsp. Convention over configuration (with not even a specified way to configure it in this case).

What breaks if you call it .jsp?

If you want the .jsp to not be visible in the URL, you can use a Filter to forward the URL internally to the JSP (if you have many), or a mapping like BalusC suggests (if you have only a few).

Thilo
"What breaks if you call it .jsp?" All my pages that already have `my.js` included? :) Technicalities aside, I personally dislike to have anything but resources with the `.js` extension inside the `src` attribute of the `<script>` tag.
Bytecode Ninja
Fair enough. So use a Filter to map /something/*.js to /something/*.jsp or the mapping BalusC describes.
Thilo