Servlets & JSP

Ritik Goel
3 min readMar 7, 2021

Servlets

Servlet Technology is used to create web applications. Servlet technology uses Java language to create web applications.

Web applications are helper applications that reside at the web servers and build dynamic web pages. A dynamic page could be anything like a page that randomly chooses a picture to display or even a page that displays the current time.

Types of Servlets

There are two main servlet types, generic and HTTP:

  • Generic servlets:
    Extend javax.servlet.GenericServlet.
    They are protocol independent. They contain no inherent HTTP support or any other transport protocol.
  • HTTP servlets:
    Extend javax.servlet.HttpServlet.
    They have built-in HTTP protocol support and are more useful in a Sun Java System Web Server environment.

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() methods based on the type of HTTP request, as explained in previous lessons.
  5. Call to destroy() method: The Web Container call the destroy() method before removing the servlet instance, giving it a chance for cleanup activity.

Sample Code

JSP

JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web pages based on HTML, XML or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP and ASP, but it uses the Java programming language.

JSP Architecture

JSP Life Cycle

There are certain phases in which this cycle is divided, defined as follows:

  1. Translation phase: where the JSP file gets converted into the servlet file.
  2. Compilation phase: where servlet file gets converted to servlet class.
  3. Classloading
  4. Instantiation of the servlet object
  5. Servlet container calls jspInit() method for initialization
  6. Servlet container calls the _jspService() for processing the request
  7. Clean up of the JSP, here the servlet container will call jspDestroy() method.

JSP Tags

  • Directives: These types of tags are used primarily to import packages. Alternatively, you can also use these tags to define error handling pages and for session information of the JSP page.
  • Declarations: JSP declarations start with ‘<%!’ and end with ‘%>’. In this you can make declarations such as int i = 1, double pi = 3.1415 etc. If needed, you can also write Java code inside declarations.
  • Scriptlets: JSP Scriptlets start with ‘<%’ and end with ‘%>’. This is where the important Java code for the JSP page is written.
  • Expressions: JSP expressions start with ‘<%=’ and end with ‘%>’. If you want to show some value, you need to put it in between these tags.

Difference between Servlets and JSP

--

--