Servlet and JSP

  • Less response time because each request runs in a separate thread.
  • Servlets are scalable.
  • Servlets are robust and object-oriented.
  • Servlets are platform-independent.
  • Servlets work on the server-side.
  • Servlets are capable of handling complex requests obtained from the webserver.
// Import required java libraries
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException {
// Do required initialization
message = "Hello World";

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Set response content type

// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");

public void destroy() {
// do nothing.
<%-- JSP comment --%>
<%out.print("Hello, Sample JSP code");%>
  • It does not require advanced knowledge of JAVA.
  • It is capable of handling exceptions.
  • Easy to use and learn.
  • It can tags which are easy to use and understand.
  • Implicit objects are there which reduces the length of code.
  • It is suitable for both JAVA and non-JAVA programmer.
  • Difficult to debug for errors.
  • First-time access leads to wastage of time.
  • Its output is HTML which lacks features.
<% Java code here %>
Java code here
<% if (doodad) {%>
<% } else { %>
<% } %>

Difference Between JSP and Servlet

Java Server Pages is a server-side programming technology that allows the creation of a dynamic, platform-independent method for developing Web-based applications. JSP have access to the whole family of Java APIs, including the JDBC API to access enterprise databases. JavaServer Pages is a technology for creating Web pages that support dynamic content. This helps programmers embed java code in HTML pages by making use of specific JSP tags, most of which begin with <% and end with %>. Servlets implement a component-based, platform-independent method for developing Web-based applications, without the performance restrictions of CGI programs. Servlets have access to the complete family of Java APIs, including the JDBC API to access enterprise databases. Servlets are platform-independent because they have drafted in Java.Java security manager on the server implements a set of limitations to preserve the resources on a server machine.

Servlet Life Cycle

  1. Loading Servlet Class: A Servlet class is loaded when the first request for the servlet is received by the Web Container.
  2. Servlet instance creation: After the Servlet class is loaded, the Web Container creates the instance of it. Servlet instance is created only once in the life cycle.
  3. Call to the init() method: init() method is called by the Web Container on servlet instance to initialize the servlet.
  4. Call to the service() method: The containers call the service() method each time the request for a servlet is received. The service() method will then call the doGet()or doPost() method based on type of HTTP request.
  5. Call to destroy() method: The Web Container call the destroy() method before removing the servlet instance, giving it a chance for cleanup.
  • Translation — JSP pages don’t look like normal java classes, actually, the JSP container parses the JSP pages and translates them to generate corresponding servlet source code. If the JSP file name is home.JSP, usually named as
  • Compilation — If the translation is successful, then the container compiles the generated servlet source file to generate a class file.
  • ClassLoading — Once JSP is compiled as servlet class, its lifecycle is similar to servlet and it gets loaded into memory.
  • Instance Creation — After the JSP class is loaded into memory, its object is instantiated by the container.
  • Initialization — The JSP class is then initialized and it transforms from a normal class to a servlet. After initialization, ServletConfig and ServletContext objects become accessible to the JSP class.
  • Request Processing — For every client request, a new thread is spawned with ServletRequest and ServletResponse to process and generate the HTML response.
  • Destroy — The last phase of the JSP life cycle where it’s unloaded into memory.
  1. jspInit() declared in JspPage interface. This method is called only once in JSP lifecycle to initialize config params.
  2. _jspService(HttpServletRequest request, HttpServletResponse response) declared in HttpJspPage interface and response for handling client requests.
  3. jspDestroy() declared in JspPage interface to unload the JSP from memory.



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