Java Server Pages (JSP) Tutorial

Introduction to JSP

  • Java Server Pages (JSP)  is a server-side scripting technology. It helps to create dynamic and platform-independent web-based applications.
  • JSP is an extension of Servlet API and given by Sun Microsystems.
  • JSP contains HTML tags and can add Java codes inside the HTML.
  • JSP separates presentation and business logic as the web designer can update JSP pages without knowledge of Java programming.
  • JSP tags start with "<%" and end with "%>".
  • JSP is used to collect input from user through web page forms, present records from a database, passing control between pages and sharing information.

Advantages of JSP

  • JSP is easy to maintain. It can easily separate business logic from presentation logic.
  • JSP is developed on Java language so it is portable to other operating systems.
  • JSP is the extension of Servlet. It has a high performance and scalability.
  • It reduces the size of code because it uses lots of action tags, custom tags etc.
  • JSP provides the fast development because if the page is modified there is no need to recompile and redeploy the application.

Architecture of JSP

jsp architecture

Life Cycle of JSP Page

There are following steps in the JSP life cycle:
  • Translation of JSP page.
  • Compilation of JSP page.
  • Loading the class.
  • Instantiation of Servlet objects.
  • Initialization by calling jspInit( ) method.
  • Request Processing by calling _jspService( ) method.
  • Destruction by calling jspDestroy( ) method.
jsp lifecycle
  • First when web browser asks for JSP, Web container translates JSP code into a servlet(.java) file, then compiler compiles it into .class file.
  • In next step the classloader loads the servlet class bytecode.
  • Web container calls the jspInit() method to load JSP class. For each request the web container invokes the _jspService( ) method.
  • Finally the jspDestroy( ) method is invoked for cleanup.

Example

jspInit( ) method

public void jspInit( )
{
    //code
}

_jspService( ) method

void _jspService(HttpServletRequest request, HttpServletResponse response)
{
     //service code
}

jspDestroy( ) method

public void jspDestroy( )
{
     //cleanup code
}