jQuery in Action, 2nd edition
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes servlet-mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "servlet-mapping" Watch "servlet-mapping" New topic
Author

servlet-mapping

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
can somebody explain how and when to use the above element with an eg
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
tony wang
Greenhorn

Joined: Apr 13, 2002
Posts: 14
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>...
...


SCJP,SCWCD
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Tony:
listener declaration is correct in your post. It comes before servlet element.
- madhav
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
if url-pattern is "/" then what does that mean??
<url-pattern>/</url-pattern>
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

    Joined: Nov 22, 2008
    Posts: 18944
    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

    Joined: Apr 20, 2000
    Posts: 3252
    ? The servlet-name in the mapping... am I missing something?
    - Peter
    Madhav Lakkapragada
    Ranch Hand

    Joined: Jun 03, 2000
    Posts: 5040

    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

    Joined: Apr 20, 2000
    Posts: 3252
    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

    Joined: Jun 03, 2000
    Posts: 5040
    Thanks, Peter.
    Your explanation sounds more logical to me, now that I understand what a "default servlet" is.
    regds.
    - madhav
     
     
    subject: servlet-mapping
     
    IntelliJ Java IDE