8. Working of Servlet and useful Interfaces terms

 Working of Servlet


How Servlet Works?

1) When the web server (e.g. Apache Tomcat) starts up, the servlet container deploy and loads all the servlets. During this step Servlet container creates ServletContext object. ServletContext is an interface that defines the set of methods that a servlet can use to communicate with the servlet container.


Note: There is only one ServletContext per webapp which is common to all the servlets. ServletContext has several useful methods such as addListener(), addFilter() etc. For now I am not explaining them as I will cover them in a separate text about ServletContext.


2) Once the servlet is loaded, the servlet container creates the instance of servlet class. For each instantiated servlet, its init() method is invoked.


3) Client (user browser) sends an Http request to web server on a certain port. Each time the web server receives a request, the servlet container creates HttpServletRequest and HttpServletResponse objects. The HttpServletRequest object provides the access to the request information and the HttpServletResponse object allows us to format and change the http response before sending it to the client.

The service() method dispatches the request to the correct handler method based on the type of request.

4) When servlet container shuts down, it unloads all the servlets and calls destroy() method for each initialized servlets.




ServletRequest Interface 

When a client sends a request to the web server, the servlet container creates ServletRequest & ServletResponse objects and passes them as an argument to the servlet’s service() method. The request object provides the access to the request information such as header and body information of request data.


RequestDispatcher methods in Servlet

The RequestDispatcher interface defines an object that receives the request from client and dispatches it to the resource(such as servlet, JSP, HTML file). This interface has following two methods:

1. public void forward(ServletRequest request, ServletResponse response): It forwards the request from one servlet to another resource (such as servlet, JSP, HTML file).


2. public void include(ServletRequest request, ServletResponse response): It includes the content of the resource(such as servlet, JSP, HTML file) in the response.



ServletResponse Interface

The servlet container is connected to the web server that receives Http Requests from client on a certain port. When client sends a request to web server, the servlet container creates HttpServletRequest and HttpServletResponse objects and passes them as an argument to the servlet service() method.

The response object allows you to format and send the response back to the client. 



HttpSession in Servlet

The HttpSession object is used for session management. A session contains information specific to a particular user across the whole application. When a user enters into a website (or an online application) for the first time HttpSession is obtained via request.getSession(), the user is given a unique ID to identify his session. This unique ID can be stored into a cookie or in a request parameter.

We can store the user information into the session object by using setAttribute() method and later when needed this information can be fetched from the session. 

This is how you store info in session. 


Cookies in Servlet

Cookies which is also used for session management.

When a user visits web application first time, the servlet container crates new HttpSession object by calling request.getSession(). A unique Id is assigned to the session. The Servlet container also sets a Cookie in the header of the HTTP response with cookie name and the unique session ID as its value.


The cookie is stored in the user browser, the client (user’s browser) sends this cookie back to the server for all the subsequent requests until the cookie is valid. The Servlet container checks the request header for cookies and get the session information from the cookie and use the associated session from the server memory.


Types of Cookies

We can classify the cookie based on their expiry time:

1) SessionCookies:

Session cookies do not have expiration time. It lives in the browser memory. As soon as the web browser is closed this cookie gets destroyed.


2) Persistent Cookies:

Unlike Session cookies they have expiration time, they are stored in the user hard drive and gets destroyed based on the expiry time.




Comments

Popular posts from this blog

2. JSP Scripting elements & Directives

1. JSP Introduction And Life-Cycle

3. JSP Action Tag and Expression Language