• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet mapping

 
Thambi Rajah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume that our deployed Web Application name is demo. When we enter the following URL (defaultURL) on the browser,
http://localhost:8080/demo/servlet/LoginServlet
it is looking for the LoginServlet.class in the directory C:\jakarta-tomcat4.0\webapps\demo\WEB-INF\classes.
Based on SCWCD Exam Study Kit page 75, we can split the URL into context path, servlet path and path info.
In this URL, context path = /demo
servlet path = /servlet
path info = /LoginServlet
I understand that the context path, /demo, is mapped to C:\jakarta-tomcat4.0\webapps\demo from an entry in server.xml(TOMCAT).
My question is how the servlet path, /servlet, is mapped to \WEB-INF\classes?

Thanks in advance,
Thambi
 
Jacky Chow
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Thambi,
It is done by set <servlet> and <servlet-mapping> in file /WEB-INF/web.xml as follow:
<web-app>
...
<servlet>
<servlet-name>anyName</servlet-name>
<servlet-class>yourClassName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anyName</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
...
</web-app>
the first "anyName" and second "anyName" must same, it is a alias of the servlet, yourClassName is the name of your servlet class name, "/servlet/LoginServlet" is the path you mentioned, an it can be any valid uri you want, such as login.html.
help this can help you.
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thambi
I'm a newbie to Servlets and also found this a bit confusing. I satisfied myself with the explanation that Jacky gave w.r.t servlet mappings specified in the deployment descriptor, and that the WEB-INF directory does not form part of the document root of the web application (see Fig 5.1 of the SCWCD Exam Study Kit).
So, unless I find out otherwise, I'm assuming that:
  • The servlet name is determined by an interpretation of the servlet-path and path-info bits of the actual URI, using the <servlet-mapping> tag(s) in the deployment descriptor
  • The container then determines the servlet class as defined in the <servlet-class> tag of the <servlet> descriptor
  • The container locates the servlet class by looking in the \classes directory under the \WEB-INF directory, where the classes should be structured according to the package they belong to


  • Probably not the most elegant way to remember it, but it does seem to work
     
    Mark Howard
    Ranch Hand
    Posts: 285
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Additionally, however, is what happens when there are no <servlet-mapping> tags in the deployment descriptor?
    Seems that the URI must then be specified with a servlet-path = "/servlet", and the remaining path-info bit ("/LoginServlet") is used to determine the servlet class in the matching <servlet> tag.
    Clear as mud
     
    Anthony Villanueva
    Ranch Hand
    Posts: 1055
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi mark, you are basically correct. For more info, you can try thislink.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic