Forums Register Login

servlet-mapping

+Pie Number of slices to send: Send
can somebody explain how and when to use the above element with an eg
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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>...
...
+Pie Number of slices to send: Send
Tony:
listener declaration is correct in your post. It comes before servlet element.
- madhav
+Pie Number of slices to send: Send
if url-pattern is "/" then what does that mean??
<url-pattern>/</url-pattern>
+Pie Number of slices to send: Send
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 ]
    +Pie Number of slices to send: Send
    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
    +Pie Number of slices to send: Send
    ? The servlet-name in the mapping... am I missing something?
    - Peter
    +Pie Number of slices to send: Send

    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
    +Pie Number of slices to send: Send
     

    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 ]
    +Pie Number of slices to send: Send
    Thanks, Peter.
    Your explanation sounds more logical to me, now that I understand what a "default servlet" is.
    regds.
    - madhav
    I wasn't selected to go to mars. This tiny ad got in ahead of me:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 740 times.
    Similar Threads
    problem in excuting Basicservlet
    Why is my Filter called for every image, included jsp and the main jsp?
    Difference Between Unidirectional and Bidirectional
    doPost
    Is there a way to Invoke a Servlet without having any Servlet Mapping Data in web.xml
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 02:09:54.