This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet mapping" Watch "Servlet mapping" New topic
Author

Servlet mapping

Thambi Rajah
Ranch Hand

Joined: Jul 19, 2002
Posts: 36
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

Joined: Sep 01, 2002
Posts: 63
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.


Jacky
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
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

    Joined: Feb 14, 2001
    Posts: 285
    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

    Joined: Mar 22, 2002
    Posts: 1055
    Hi mark, you are basically correct. For more info, you can try thislink.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Servlet mapping
     
    Similar Threads
    Basic Servlet Problem
    my first servlet please help.
    How to invoke a servlet?
    Running self made Servlets on tomcat
    question about JSP - Tomcat