Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Friday, 15 February 2013

Jsp and Servlet Tutorials - Request-Response Model (Part 1 of 8)

Today we are living in an era where java is being used at its best, we have very efficient frameworks and tools to make web development easy and stable. To make software development more useful and productive its important to implement industry best practices and clean coding implementations. Good naming conventions and easy to implement algorithms is not enough to make smart development done. 

No matters how improved frameworks and techniques we are using in our development process the key feature to write smart code is always stick to basics.

There is no doubt that a lot of effort and experience is behind today's java web frameworks and these stuffs has been made web development very flexible and advanced, but we must not forget about those old but gold concepts so called jsp and servlets. Whenever there is something related to web development in java the first thing that comes in our mind is servlets and jsp's. So its never wrong to say that a good java developer must have a very good command on basics of servlet and jsp's.     

In this series of jsp-servlet tutorials we are going to talk about servlet ans jsp's to make you guys understand the basics of what web development is all about. In today's discussion lets starts with an introductory part of Servlets. By the end of this tutorial we will be able to understand things like : What is a Request ?, What is a Response and What is Request-Response model exactly.

Although this series of jsp-servlet tutorials is written for extremely start level readers, but we assumes that you guys have a little knowledge of oops concepts and java language in general. First of all lets starts with a quick and brief introduction to data flow model of web to better understand the things that are exactly happening behind the screen.

Request-response model

jsp-servlet-tutorials

Whenever a software guy enters in web development world the first thing that he comes to know is, client and server, client makes an request and server sends a response back to client. So let's first discuss how basically communication takes place in web world and what exactly the terms client and server means.

Client: A client in basically something(web browser) or someone(user) who requests some resource from server.

Server: A server is a combination of a hardware machines and a number of softwares running on that machine. The one and only duty of server is to serve resources that are being requested by the client.

Servers itself are capable of serving static resources only. To serve a dynamic response servers needs some extra technologies like servlets running on them. 

How servlets helps server in serving dynamic contents, we will come to know about it in later part of this blog.
Now to get client and server in contact and make communication possible we needs some set of rules so called http(Hyper text transfer protocol). In web there are a number of protocols other than http that does the communication work done, but almost in 99% of applications the requests being made are http requests.

Http: Http can be assumed as a common interface of interaction that both client and server understands.

Request/Response circle
 
Web flow starts from a request being made by user's browser, the request is made as http request so that server can understand it. Based on request the server searches for an appropriate resource and sends it back to client in form of http response.

Http Request: A http request basically have three major components.
1 - HTTP method, there are 7 methods defined in java servlets but most of the time you will see either a get or post method. We will get to know about these methods and their usage in later part of this blog.
2 - The requesed page URL, the page to access like www.google.com.
3 - Parameters, parameters(as id, name, email.. etc.) are being send as part of request on which the response is being generated.

Http Response: A http request basically have three major components.
1 - A status code, this code tells the browser wheather the request is successfull or not.
2 - Content type, it tells the browser about the type of content that response web page contains in it (text, picture, html...etc).
3 - The content, the important and last information that is the served resource that the user was requested.

Till now we are much famelier with the request-response cycle and client-server model over the web. Now the next things that would be coming in your mind is "Where does the Servlet comes into the picture". As we said before that the server can serve static pages or response by itself. In case we need to serve some dynamic pages the served must have something that provides dynamic contents to serve. And this is what servlets does exactly.
Servlets are nothing but helper applications that helps the server to response back dynamic pages. How servlets prepares dynamic response and what exactly happened in the process we will come to know about this thing in a moment, be cool.

A static resource is a page whose contents are static in nature as a simple text file, on the other hand dynamic pages are being prepared according to user's request parameters.

For example lets assume a user requests a page having some text information written on some topic. This page content is never going to change and the same page is sended back to every user request.
But if a user requests something like profile information of his own account this time server needs to create a page of that user's information based on id or username the user provided. This page is going to contain different profile information for different users. These username, email are called parameters and the response id being generated dynamically based on user's request parameter. These are called dynamic pages.

The existence of servlets came into picture because, most of the time client needs dynamic contents and not static contents always. So in java when we need some dynamic content rather that static content we go for a servlet. 

In this part of jsp-servlet tutorial series we came across a basic introduction of Servlet and come to know about request, response and request-response model in web development. In upcoming blogs of this tutorial series we will come to know about Servlet Architecture and other topics related to Servlet ans Jsp's.









Thanks for reading !
Being Java Guys Team



2 comments:

  1. Nagesh,

    Your blog is really informative and will help many people. Your explanation is nice and to the point. Thank you.

    ReplyDelete
  2. Hi Nagesh,

    You blog is great and informative. This post was helpful for me. But I request you to make the font size a bit more larger. Thank you.

    - Rohan.

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft