Wednesday, 8 July 2015

How does a request Url is mapped to a specific Servlet in Java

The "<url-pattern>" element of a "<servlet-mapping>" or a "<filter-mapping>" associates a filter or servlet with a set of URLs. Which servlet should get called can be checked from the entry of web.xml file.

There are two steps to check which Url will mapped to which Servlet

1) Look for the corresponding entry of Url inside web.xml (deployment descriptor)
2) See the rules for mapping inside web.xml

Lets see how Url is being mapped to correct Servlet.

1) Look for the corresponding entry of Url inside web.xml (deployment descriptor)

Inside web.xml file there is a specific tag available called "<url-pattern>", this is the tag used to map any request to a specific "Servlet".

Here are three sample entries of three different Servlets.

<servlet>
<description></description>
<display-name>Marks</display-name>
<servlet-name>Marks</servlet-name>
<servlet-class>com.registration.Marks</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Marks</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<servlet>
<description></description>
<display-name>StudentData1</display-name>
<servlet-name>StudentData1</servlet-name>
<servlet-class>com.registration.StudentData1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentData1</servlet-name>
<url-pattern>/StudentData</url-pattern>
</servlet-mapping>

<servlet>
<description></description>
<display-name>StudentData</display-name>
<servlet-name>StudentData</servlet-name>
<servlet-class>com.registration.StudentData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentData</servlet-name>
<url-pattern>/StudentData/*</url-pattern>
</servlet-mapping>

<servlet>
<description></description>
<display-name>Registration</display-name>
<servlet-name>Registration</servlet-name>
<servlet-class>com.registration.Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/StudentData/Registration/*</url-pattern>
</servlet-mapping>

We have three servlets called

1) Marks
2) StudentData
3) Registration

For every servlet we have declared a corresponding <servlet-mapping> entry which will relate Url -> Servlet with the help of "<url-pattern>" element of "<servlet-mapping>".


2) See the rules for mapping inside web.xml

Here are the mapping rules which container follows for mapping to any Url with a specific Servlet.

The container,

i)   gives precedence to an exact path match over to wildcard path match
ii)  prefers to match the longest pattern
iii) prefers path matches over filetype matches

3) Lets see some examples of Url -> Servlet mapping (Assuming web.xml entry mentioned in point #1)

http://student.com/student/StudentData1 matches to "StudentData1" servelt (Rule #i)
http://student.com/student/StudentData/abc matches to "StudentData" servelt (wild card match)
http://student.com/student/StudentData/Registration matches to "Registration" servlet (Rule #ii)
http://studnet.com/student/abc.jsp    matches to "StudentData" Servlet (Rule #iii)
http://student.com/studnet/StudentData/Registration/abc matches to "Registration" servelt


Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics

No comments:

Post a Comment