File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Wildcard servlet  mappings in web.xml - how to get the part behind the * ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Wildcard servlet  mappings in web.xml - how to get the part behind the * ?" Watch "Wildcard servlet  mappings in web.xml - how to get the part behind the * ?" New topic
Author

Wildcard servlet mappings in web.xml - how to get the part behind the * ?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there,

I have a question about wildcard mappings for servlets.
I have a servlet that is mapped to /resource
It create dynamic images. To get an image I call:
/resource?skin=summer&scope=full
For a test I have put Apache HTTP in front of the J2EE container and use a mapping, so I can call
/resource/summer/full
Can I "move" the mapping from the HTTP configuration into the web.xml file? How would I do that?
Thx for helping!
-------
Clarification:
I know, that i can map the servlet to resource/*
How can I retrieve the part that represents the * ?
-------
:-) stw
[ October 01, 2004: Message edited by: Stephan H. Wissel ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
dont know whether i got you or not. sorry if I got wrong.

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/test/test/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/next </url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet3</servlet-name>
<url-pattern>*.jsp </url-pattern>
</servlet-mapping>

these are some examples with different URL patterns. <servlet-mapping> is a direct child of <web-app>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Adeel,

thx for the reply. My question was not(?) regarding the web.xml (I suspect) buth rather about retrieving the wildcard value.
My mapping looks like

So the servlet gets invokes by all of the following
/resource/summer/holidaycontinents
/resource/winter/airlines
/resource/logo
/resource/company/ceo
... etc
What I want to know is how to get hand on the part that is represented by summer/holidaycontinents
winter/airlines
logo
company/ceo
in my servlet?
:-) stw
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
If none of the rules (mapping)results in a servlet match, the container will attempt to serve content appropriate for the resource requested.

If a "default" servlet is defined for the application, it will be used. But dont know how to specify a servlet as default
[ October 02, 2004: Message edited by: adeel ansari ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Adeel,

thx for the reply. Unfortunatly this is not the answer I'm looking for. My servlet get's invoked as planned, so my web.xml uri-pattern works perfectly. What I need to know: how do I get to know INSIDE my servlet code what the URL was that triggered the servlet. AND I'm looking for a way to make that rather dynamic than hardcode it. So...
what would I code in Java code if I want to know what the * part of the URL was that actually loaded my servlet!
stw
Richard Bradford
Ranch Hand

Joined: Apr 20, 2004
Posts: 48
Have a look at the HttpServletRequest api, in particluar getContextPath()
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Richard, hi Adeel,

problem solved! Thx for helping! This are my findings:

1) getContextPath() returns the Application Context. That is the (user visible) path in a browser the whole web application has been deployed. When e.g. your URL is:
/TestCollection[/b]/test1/winter/bla]http://localhost:8080/TestCollection/test1/winter/bla
the bold part is what getContextPath() returns.
2) The function getPathInfo() does the trick!
My servlet description in the web.xml loos like that:


Given again the URL
/winter/bla[/b]]http://localhost:8080/TestCollection/test1/winter/bla
the bold part is what getPathInfo() returns.

Important Detail!!!
The mapping in web.xml is /test1/*
Nevertheless you can call the servlet both with .../test1 and test1/ !!
In the first cast getpathInfo is null, so you need to test for it!

Some more insights:
URL: http://localhost:8080/TestCollection/test1
getPathInfo(): null

URL: http://localhost:8080/TestCollection/test1/
getPathInfo(): /


URL: http://localhost:8080/TestCollection/test2
getPathInfo(): null

URL: http://localhost:8080/TestCollection/test2/
Resource not available error

URL: http://localhost:8080/TestCollection/test3
Resource not available error

URL: http://localhost:8080/TestCollection/test3/
getPathInfo(): null


URL: http://localhost:8080/TestCollection/test3/anything
Resource not available error

Hope this is helpful if someone is searching...
:-) stw
[ October 02, 2004: Message edited by: Stephan H. Wissel ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
sorry didn't get your actual question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wildcard servlet mappings in web.xml - how to get the part behind the * ?