URL Mapping in Servlets.

Hello Friends,

This is one of the interesting tutorials regarding the URL Mapping in Servlets. The thing which makes this interesting is that, the URL that one click on the browser is not the real one but still it displays the result on his interest. This is no magic but the advantage of Servlet that avoids the direct linking to the actual page for the client.

The URL comes within the Servlet Request whenever the link is clicked (or the Submit button is clicked or similar event that navigates the browser to a new page). Now, the question is that How Container knows which url should be mapped with the response. The answer for this is present in the Deployment Descriptor or DD. In our previous tutorial on Hello World program, we have written on web.xml file which is nothing but the DD. This simple xml file or DD tells the container how to run the servlets. However, DD is used for number of purposes besides url-mapping, but we will now focus on url mapping itself.

There are two elements of mapping mostly used in DD one for mapping the URL with the internal name and other for mapping internal name with the class file that needs to be executed. Internal name is the name given to specific servlet that maps the servlet with the specific url, it is only used by DD and client does not know anything about this. Let us consider the example from the previous example. Below are the web.xml file and its contents.

In the above snippet, the code between the tags <servlet> and </servlet> is used to map the internal name with the fully qualified class name. This <servlet> element tells the container about which class file belongs to which web application and displays the contents accordingly. Here <servlet-class> defines the fully qualified class file which needs to be executed, and <servlet-name> defines the internal name for the servlet, it must match the <servlet-name> of the <servlet-mapping> element.

The other part between <servlet-mapping> and </servlet-mapping> elements is used to map the url with the internal name.

Here, the element <servlet-name> is the same as that of the first part this helps the container to map the specific .class file to be executed on the particular url request. The element <url-pattern> is the one that is only visible to the client as it is the actual URL which user clicks on. Remaining part of the xml file is only up to the container and has nothing to do with client. So, from the above xml file we ca say that “First Servlet” is the Internal name (not real), “FirstServlet” is the class file created from FirstServlet.java file without .class extension and “/Servlet1” is the path used in the actual URL that will be used by the client. The url used by the client in this case will be “localhost:8080/context-path/Servlet1” where context path is the path of you web app.


Hope this will help you.

Thanks and Regards,


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.