• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet-mapping

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody explain how and when to use the above element with an eg
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classic definition.....
Used to define a mapping between a servlet (identified by a servlet-name tag) and a URL (identified by a URL-pattern).
Say, you want all URL's to your site of the form .../*.abcd to invoke a servlet called "MySpecialApplication.class" then you do the following in your web-app element of the DTD...

<servlet>
<servlet-name>MySpecialApplication</servlet-name>
<servlet-class>com.myapp.MySpecialApplication</servlet-class>
<servlet>
[...snip...]
<servlet-mapping>
<servlet-name>MySpecialApplication<servlet-name>
<url-pattern>/*.abcd</url-pattern>
</servlet-mapping>
regds.
- madhav
 
tony wang
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
servlet-mapping element must be used after the declaration of the servlet element mapped, like:
...
<servlet>
<servlet-name>a_let</servlet-name>
...
</servlet>
...
<servlet-mapping>
<servlet-name>a_let</servlet-name>
...
</servlet-mapping>
...
A fuller scene is:
<web-app>
<display-name>...
<description>...
<distributable/>
<context-para>...
<listener>... <!-- not sure -->
<servlet> ...
<servlet-mapping>...
...
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony:
listener declaration is correct in your post. It comes before servlet element.
- madhav
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if url-pattern is "/" then what does that mean??
<url-pattern>/</url-pattern>
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Servlet spec, SRV.11:
[...] The first successful match is used with no further matches attempted:
  • The container will try to find an exact match of the path of the request to the path of the servlet. [...]
  • The container will recursively try to match the longest path-prefix [...]
  • If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. [...]
  • If neither of the previous three rules result 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.
  • [...] A string containing only the �/� character indicates the "default" servlet of the application. [...]
    The emphasis is mine. Pretty straightforward, as far as specifications go. If you haven't downloaded the Servlet 2.3 and JSP 1.2 specifications yet, please do so. Studying for the certification is going to be impossible without them.
    - Peter
    [ April 15, 2002: Message edited by: Peter den Haan ]
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If a "default" servlet is defined for the application, it will be used
    how can I say that a particular servlet is default..where can I specify this...(in web.xml) or anywhere else
     
    Peter den Haan
    author
    Ranch Hand
    Posts: 3252
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ? The servlet-name in the mapping... am I missing something?
    - Peter
     
    Madhav Lakkapragada
    Ranch Hand
    Posts: 5040
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    how can I say that a particular servlet is default..where can I specify this...(in web.xml) or anywhere else
    [ ---- snip ------ ]
    ? The servlet-name in the mapping... am I missing something?


    IMO.....If a servlet-container does not find the servlet specified in the servlet-name, then the "default servlet" is used.
    A "default servlet" declaration is Servlet-container specific. Hence the spec doesn't specify how or where to declare this, atleast that's what I think.
    Having said that, for Tomcat, the default application is "webapps/Root". I don't think there is a "default servlet" defined for Tomcat.
    regds.
    - madhav
     
    Peter den Haan
    author
    Ranch Hand
    Posts: 3252
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Madhav Lakkapragada:
    IMO.....If a servlet-container does not find the servlet specified in the servlet-name, then the "default servlet" is used.
    Not quite - that's a plain ol' configuration error. The default servlet is what gets invoked if a request comes in that doesn't map to any other servlet.
    A "default servlet" declaration is Servlet-container specific.
    Not at all. My apologies, the explanation I quoted above is more opaque than I initially thought. To give an example,Turns the servlet called "MySpecialApplication" into the default servlet for the web-app. Any request that does not match a more specific servlet mapping will end up at MySpecialApplication. (note: a servlet mapping can be implicit, such as *.jsp; i.e. jsps will still be processed by the JSP engine).
    - Peter
    [ April 15, 2002: Message edited by: Peter den Haan ]
     
    Madhav Lakkapragada
    Ranch Hand
    Posts: 5040
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, Peter.
    Your explanation sounds more logical to me, now that I understand what a "default servlet" is.
    regds.
    - madhav
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic