Servlets
Request-response model
This shows how basically communication happen in the web.
There are two main roles named client and the server. Client is a someone or something requesting some resource and the server is serves the request which is requesting by the client. As an example, I want check my email in Gmail. So, I am nothing but the client to the Gmail. Gmail server is a web server or http server. http server means a machine which can take a request from the client and can give back a response. This response is static html page not dynamic.
When I logon to gmail.com, sends
a request to the Gmail server over the web. This request is http request.
http(Hyper Text Transfer Protocol) is a protocol. Everything communicate
through the web should be http . This web server can only understand http. When
a request comes to the server, it checks the page you requested in repository.
Then it picks one static page and sends it to the client. This is also a http
response. The client here is nothing but a web browser.
Here
the web server is a machine which gets http requests from a client and response back a static html file. This file may be any
type like jar, image, video etc.
Till
now we have discussed only static pages. But what happen when the server
basically the client needs a dynamic page. As an example, when you type Gmail, the first
page which you get is a static page. Because of all people in the world will
get that same page when they try to logon to gmail. After you type your email
and password, you will be got your inbox. That page is not a static page. It is
a dynamic page. Because of there is no chance to get a same inbox page for two
different people. Everyone will be got their own inbox page.
But
this web server can only handle/ serve static pages. It doesn’t have an ability
to develop dynamic pages. So, how to develop dynamic html pages in here? This
is where servlets come in to the picture.This web server application can only serve static html pages. So, when a request comes from the client requesting dynamic page, it knows that can’t handle that request. At that moment, the helper application comes in the picture. It can handle dynamic requests. This is helper application is nothing but the servlet. So, we need the help of servlets when we want a dynamic content.
Now let’s see how the flow works between the client and the server.
When a request goes, the type of the method also sent with it. It is GET or POST. Now we assume this type is GET. Client sends a request to the web server requesting a dynamic page. Its type is GET. But the web server can’t develop dynamic page. Therefore, the server gets the help of servlet to develop that requested dynamic page. Actually, the servlet is nothing but a Java program resides inside the server. But it is a special Java program. It hasn’t main() method, has some call back methods. So, how does the server communicate with the servlet? The answer is web container. It is also called as servlet engine. The web container helps web server to communicate with servlet. Basically servlets live and die within the web container. Web container has responsible for invoking methods in the servlet.
Now let’s see this process in more detail.
Client sends a request to the web Server and it forwards to the Web Container. Web Container responsible for communication with the servlet and allows it to build a dynamic response. But the requests go from the web container to the servlet and from the server to the container are different. Web server forwards a HTTP request sent by the client. But the web container doesn’t forward it in same format. Because of servlet is a Java program. It can’t understand HTTP format. Only understand Java objects. Therefore, the web container converts the HTTP request into the request object and it also creates a response object. Then sends both objects to the servlet. Here the web container will create a thread for each request. As well as it calls call back methods. Web container knows what call back methods should call. If the request is type “GET” the it call doGet() method. Basically we write the whole code for the dynamic content within the doGet(). This response object sends to the web container. It is also a Java object. Therefore, it is converted into HTTP response and sends back to the client.
How does the web container know
which servlet can serve the request requested by the client?
Imagine we have many
servlets in our application which do different kind of jobs. Then there should
be some mechanism to understand to the web server which servlet can serve the
particular request. Basically, there is a file called “web.xml”. it is also called
as “deployment descriptor”. This is the main file for the web container. Above
figure shows you an example of the web.xml. as you can see there, it has some
elements like <servlet>, <servlet-mapping>.
Every servlet in the
application should have an entry into this file. We put basic information about
the servlet inside the <servlet>. Inside the <servlet-name> we put
a simple readable name for the servlet(Greeter). Inside the
<servlet-class> we put the path to that
servlet(org.apache.hello_world_soap_http.GreeterImpl). How to know when a
request comes, it is for this particular
<servlet-class>? It is derived by the <servlet-mapping>. In here,
<servlet-name> should be same as the <servlet-name> in the
<servlet> element. <url-pattern> is the url of the request sent by
the client.
So, normally there
are many <servlet> and <servlet-mapping> elements in the web.xml.
If we consider this example, when a request comes with the url pattern like http://www.abc.com/Greeter then the web
container searches is there any <url-pattern> inside the
<servlet-mapping>. You can see there is a such pattern. Then container
reads the <servlet-name> of that pattern. After it goes and looks for the
similar <servlet-name> inside the <servlet> element. You can see
there is a similar <servlet-name> inside the <servlet>. Then
container goes along the path defined by the <servlet-class> of that
<servlet-name> and finds the particular servlet.
No comments:
Post a Comment