HTTP is a stateless protocol; each time a client requests for a page, a separate connection is established between the client and the server. Thus, it provides no way for a server to maintain information for a particular user across multiple requests. There are many web applications where it is required to maintain this information. For example, in case of shopping cart, it is required to keep track of the list of items that are added in each user's cart. For this, the server must provide a way to store data for each client and distinguish clients from one another.
In a web server, a session is a collection of all the requests made by a client (browser). HTTP is a stateless protocol. Between requests, it does not maintain any state of the client (browser). Suppose the client makes request for a web page, it should be checked whether the request is from an authorized user. It is impossible to check the authorization for each web page. It is necessary to maintain some information of the user while a user navigates between web pages. Thus, it is useful to maintain the state of client. The following processes arc used to maintain the state of a client:
In order to read cookies that come back from the client (browser) in request header, you need to call getCookies () method of the HttpServletRequest. If the request contains no cookies this method returns null.
The Hypertext Transfer Protocol (HTTP) is the network protocol that the web servers and the client browsers use to communicate with each other. The HTTP is a stateless protocol. A client browser opens a connection and requests for a resource from the web server. The web server then responds with a requested resource and closes the connection.
This method sends a temporary redirect response to the client using the mentioned redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading ‘/’the container interprets it as relative to the current request URL. If the location is relative with a leading ‘/’the container interprets it as relative to the servlet container root.
This method forwards a request from a servlet to another resource (servlet, JSP file or HTML file) on the server. It enables one servlet to do prelude processing of a request and another resource to create the response.
This method of RequestDispatcher interface includes the content of web resource (servlets, JSP and HTML file) in the response. In other words, this method allows server-side to include the response of destination program to source program. Here ServletResponse object are passed as the argument of include () method.
In the diagram Below, Srvl program forwards the request to Srv2 only when the generated square value is less than 100,otherwise, the Srvl directly sends response to browser window displaying that square value.
lnvokable on both request, servletContext object. Expects URL pattern of destination JSP or HTML programs as argument value. Generated RequestDispatcher object can point the destination servlet JSP program and HTML program.
While building a complex web application there might be a need to distribute the request to multiple servlets. This is where request dispatching comes into use. Due to this requirement Servlet container supports request dispatching within the same context.
• In any mode of servlet chaining, all servlet programs/web resource programs use the same request and response objects. If srvI, srv2, srv3 and srv4 servlet programs are in forwarding request mode of servlet chaining, the html output of srv1, srv and srv3 is discarded and only the output of srv4 servlet program goes to the browser window.
Taking a request from a browser window and processing it by using multiple servlets as a chain is called Servlet Chaining. In servlet chaining, communication occurs between servlet chains and servlet programs to process the request given by a client.
Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.