Introduction to Servlet & JSP :

Hello friends, are you interested in developing web applications in Java and you heard about Servlets and JSP and you want to know what they mean, then this article is for you. In this article, we will talk about Servlet & JSP, why we use Servlet & JSP, and which one is better.

What is servlet?

Servlet is a simple java program that runs on the server and capable of handling requests and generating dynamic responses. Or we can say, A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

Servlet technology is used to create dynamic web applications. Servlet takes the request from the client on the internet and it can process that request and provide a response in the format of HTML page to the client. Now, let's discuss the life cycle of the servlet.

Servlet life cycle:

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.

· The servlet is initialized by calling the init() method.

· The servlet calls service() method to process a client’s request.

· The servlet is terminated by calling the destroy() method.

· Finally, the servlet is garbage collected by the garbage collector of the JVM.

Servlet Architecture:

· The user sends HTTP requests to the web server.

· The server has a web container containing servlet, which gathers data from the database and creates a response.

· The response created by servlet is sent through HTTP Response to the client browser.

The question arises that how is the servlet’s response converted to HTTP Response format? As the web server works purely on the HTTP protocol. Hence, this conversion from servlet’s response to HTTP response is taken care by the web container.

How Servlet Execute:

Execution of Servlets involves six basic steps:

(1) The clients send the HTTP request to the web server.

(2) The web server receives the HTTP request.

(3) The web server passes the request to the corresponding servlet.

(4) The servlet processes the request and generates the response in the form of output.

(5) The servlet sends the response back to the web server.

(6) The web server sends the HTTP response back to the client and the client browser displays it on the screen.

What is JSP?

JSP stands for Java Server Page. It is used to create web applications like servlet technology. It can be thought of an extension to servlet technology because it provides more functionality than servlet. just like JSTL (JSP tag library), expression language. JSP page consist of HTML tags and JSP tags. JSP pages are easier to maintain than servlet because we can separate designing and development.

JSP life cycle:

A JSP life cycle is defined as the process from its creation till the destruction. This is similar to a servlet life cycle with an additional step that is required to compile a JSP into servlet.

How JSP Execute:

Execution of JSP involves six basic steps:

(1) JSP page is translated into Servlet by the help of JSP translator.

(2) Servlet page is compiled by the compiler and gets converted into the class file.

(3) Instantiation (Object of the Generated Servlet is created).

(4) Initialization (the container invokes jspInit () method).

(5) Request processing (the container invokes _jspService () method).

(6) Destroy (the container invokes jspDestroy () method).

When to use Servlet & JSP?

Servlet and JSP are two of the most popular Java web technologies to generate dynamic content in Java web applications there is some key difference between them. As per differences and our requirement we should choose Servlet & JSP. Let's have a look at the differences between Servlet & JSP :

1) The first and foremost difference between Servlet and JSP is that a JSP is a web page scripting language that can generate dynamic content while Servlets are Java programs that are already compiled which also creates dynamic web content.

2) Servlet is a java code and JSP is a HTML based code.

3) Servlet plays a controller role in the MVC approach but JSP is the view in the MVC approach for showing output.

4) Modification in Servlet is a time-consuming task because it includes reloading, recompiling, and restarting the server. But JSP modification is fast, just need to click the refresh button.

5) The advantage of JSP programming over Servlet is that we can build custom tags that can directly call Java beans. There is no such facility in servlets.

With this, we come to an end to this blog. I hope you got a clear understanding of the Servlet & JSP from this article.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store