JSP

Introduction:


  • JSP technology is used to create dynamic web applications. 
  • JSP pages are easier to maintain then a Servlet. 
  • JSP pages are opposite of Servlets as a servlet adds HTML code inside Java code, while JSP adds Java code inside HTML using JSP tags. 
  • Everything a Servlet can do, a JSP page can also do it.

  • JSP enables us to write HTML pages containing tags, inside which we can include powerful Java programs. 
    • JSP becomes a Servlet
    • JSP pages are converted into Servlet by the Web Container.
    • The Container translates a JSP page into a servlet class source(.java) file and then compiles into a Java Servlet class.



Why JSP is preferred over servlets?

  • JSP provides an easier way to code dynamic web pages.
  • JSP does not require additional files like java class files, web.xml, etc
  • Any change in the JSP code is handled by Web Container(Application server like tomcat) and doesn't require recompilation.
  • JSP pages can be directly accessed, and web.xml mapping is not required like in servlets.


Advantage of JSP

  • Easy to maintain and code.
  • High Performance and Scalability.
  • JSP is built on Java technology, so it is platform-independent.


Lifecycle of JSP

  • A JSP page is converted into Servlet in order to service requests. 
  • The translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP Lifecycle is precisely the same as the Servlet Lifecycle, with one additional first step, which is, translation of JSP code to Servlet code. 

  • Following are the JSP Lifecycle steps:
    • Translation of JSP to Servlet code.
    • Compilation of Servlet to bytecode.
    • Loading Servlet class.
    • Creating a servlet instance.
    • Initialization by calling jspInit() method
    • Request Processing by calling _jspService() method
    • Destroy by calling jspDestroy() method
Example:
in head tag
    <%
       int count = 0;
    %>
in body tag
        Page Count is: 
        <% out.println(++count); %>
 

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form