Friday 30 August 2013

How does the container know which servlet the client has requested for?

How does the container know which servlet the client has requested for?

A Servlet can have 3 names

1. client known URL name.
2. Deployer known secret internal name (servlet name and file name
3. Actual file name



  • there might be more than one servlet in web container which can do different functionality.

how does the web container knows which servlet  can help that request?
Ans: just take a example: when you take yahoomail.com, type username and password and sends the request to the server. now how does the web container knows which particular servlet in the web application for that yahoo email. there is a servlet for yahoo email, for example that servlet is yahoo servlet
now the container has to know that this yahoo servlet can handle the request which are sent by the client for yahoomail.

  • now lets see how does happen: basically there is a file call web.xml (this is also called the deployment descriptor
  • so web.xml is the main file for web container (master file for web container)
  • basically you have information about the servlet in web.xml file

as you can see the web.xml file below:
<web-app>
----
<servlet>
<servlet-name>Loginserv</servlet-name>
 <servlet-class>com.Login</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>LoginServ</servlet-name>
  <url-pattern>/Logon</url-pattern>
 </servlet-mapping>
------
------
</web-app>


  • every servlet in the web application should have an entry into this file the web.xml

for example if you observe the tags, Loginserv which handles the requests the yahoo login

  • i will have the entry for that login servlet in web.xml file so there is a servlet element , servlet name and servlet class(<servlet-name>Loginserv</servlet-name> <servlet-class>com.Login</servlet-class>) so my servlet is Login servlet, so i will write the path for the servlet which is com.Login and i also give some simple name that is Loginserv some readable name to that servlet which im refering. 
  • Now you can also see the another section that is servlet-mapping as below:

<servlet-mapping>
 <servlet-name>LoginServ</servlet-name>
  <url-pattern>/Logon</url-pattern>
 </servlet-mapping>

  • now we have mapped to some servlet class to some servlet name to this element i.e <servlet-name>Loginserv</servlet-name>

 <servlet-class>com.Login</servlet-class>

  • now servlet that can known depending on name which servlet class to invoke and where it is located
  • and how will the servlet know the request was yahoomail.com\login, how will it know for that request is for this servlet class so that is derived in the servlet mapping element
  • again the servlet mapping element we have two tags <servlet-name>LoginServ</servlet-name>

  <url-pattern>/Logon</url-pattern> here the <servlet-name>LoginServ</servlet-name> matches the servlet tag <servlet>LoginServ</servlet> tag which you have given.
and the <url-pattern> is nothing but the url the client has sent the request when the client sends yahoomail.com/logon, it actually matches that URL in the url pattern tag. It sees the client has selected this url <url-pattern>/Logon</url-pattern> so it matches this url present in the mapping tag

  • now it goes to the servlet element <servlet> <servlet-name>Loginserv</servlet-name>
  • and apart from these servlet and servlet mapping elements there might be a number of servlets and servlet mapping elements. so it checks all the servlet elements and matches this servlet name i.e <servlet> <servlet-name>Loginserv</servlet-name> and the this servlet name i.e <servlet-mapping> <servlet-name>LoginServ</servlet-name>
  • now it goes the whats the servlet class and at particula servlet name and it goes to this path i.e com.Login and ultimately it takes that servlet and invokes the methods of that servlets
  • processes the request and sends the response back to the client
  • so this is how a webcontainer can match the request sent by a client url is a valid servlet which is actually serving the request sent by the client

why do we have the URL name?
Ans: we dont want to type whole name in the URL path( ex: con.yahoomail.login details....etc). that is you want to give the client to simple name(yahoomail.com) and second reason is security, that is i dont want to know whole pathe to others, ones the servlet path knows to someone it leads to security problem.

You may like the following posts:





No comments:

Post a Comment