8

(Should be an easy one-)

How can I redirect all urls of the pattern yada*.js into a my specific jsp.
will this addition to my web.xml work:

<servlet-mapping>
        <servlet-name>MySpecific.jsp</servlet-name>
        <url-pattern>yada*.js</url-pattern>
</servlet-mapping>

or perhaps I must use javax.servlet.filter for that purpose?

Spiderman
  • 9,602
  • 13
  • 48
  • 56

2 Answers2

13

I'd drop those files in a folder called /yada and then use an url-pattern of /yada/*.

If you don't want to do that, then hand-determining it in a filter is indeed the only resort.


Update: as per your update, you actually have a second question which wasn't directly obvious from your initial question: "How to declare a JSP file as a servlet?". The answer is: use <jsp-file> instead of <servlet-class>.

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

Nevertheless, as stated in the comments, this is not the best practice. This smells to raw Java code in a JSP file which you should avoid to all extent.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • but can servlet-name attribute direct to a jsp file and not a servlet? – Spiderman Aug 05 '10 at 12:51
  • Oh, I thought you already have a `` for that. No, you can't do that alone. Use `` instead of `` in `` and it'll work. It's however not the recommend way. JSP is the wrong tool for this. Raw Java code in JSP should be [avoided](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files) to all extent. – BalusC Aug 05 '10 at 12:55
  • @helios: Ones asking this kind of questions often do :) – BalusC Aug 05 '10 at 13:00
  • That' the solution I used eventually. – Spiderman Aug 08 '10 at 14:11
1

Depending on the server the things that you can put in url-pattern are very limited.

Two are valid:

  • an absolute path (no wildcards)
  • *.ext

So that specification is not matching. I'd use a filter indeed.

PS: don't forget to specify <%page sourceEncoding=... contentType=... %> in the generating JSP :). And content-type should include charset=xxxxx

helios
  • 13,574
  • 2
  • 45
  • 55
  • ok but how do I forward to JSP from a filter. I do I have to call the 'chain.doFilter()' after the forward command or before. and do I have to call it if the pattarn was found or only if the pattern was not found – Spiderman Aug 05 '10 at 13:20
  • I continue this thread in http://stackoverflow.com/questions/3415240/forward-request-from-a-filter since I currently can't write more than a comment in this thread and the issue is not solved yet – Spiderman Aug 05 '10 at 13:44