JSP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSP Life Cycle

JSP Life Cycle

shape Description

JSP Life Cycle is like the servlet, each JAVA EE part will have its own particular life cycle. A JSP life cycle can be portrayed as the entire system from its introduction till the decimation which resemble a servlet life cycle with an additional step which is required to arrange a JSP into the servlet. JSP life cycle will comprises of three strategies as follows. While executing the JSP file, in JSP life cycle some steps will be involved as follows.

shape Conceptual figure

The below figure describes the  JSP life cycle from initialization till the destruction .
  • Translation : JSP pages will be translated in to the servlet pages, servlet executes whenever the request arrived form the client browser. Here syntactical errors will checked and converted to servlet source file.
  • JSP compilation : After successful translation.Servlet will be compiled by using  JAVAC compiler.
  • Initialization: Here container will load the class and creates the instance for that class and calls the jspinit() only once for a servlet instance used for giving initializing information. The structure of jspinit() as follows. [java]<%!public void jspinit[] { //initializtion code will be written here. }%>[/java]
  • Invoke _jspService()  : _jspSevice() method is equivalent to service() of a servlet and it is called for each request, makes the implicit objects available to the JSP page .
  • Invoke jspDestroy()  : Finally container calls the jspDestroy(), it removes the  JSP from servicing the request.The structure of jspDestroy() is as follows. [java]<%! public void jspDestroy() { } %>[/java]

Directory Structure of JSP

shape Structure

To run a simple JSP page,there is no need to create directory structure. Select a JSP file, write JSP code on that file and deploy in the server. JSP file will run without any errors, but if bean class and Servlet are used, then directory structure is required.

Steps to execute JSP

shape Steps

  • Install the server and start server.
  • Place the JSP page under the server like tomcat server.
  • Type the url like "http://localhost:portno/rootfoldername/jspfilename". Ex: http://localhost:8080/Jspapplication/index.jsp.

index.jsp

shape Example

Here used scriplet tag to print the data.
<html>
<body>
<% out.println(<style="color: #ff0000;">10<style="color: #000000;">10); %>
</body>
</html>
All the generated Servlets implement JSP page interface.Servlet interface a JspPage interface ( jspInit() & jspDestroy() ) a HttpJspPage interface ( _jspService() ) In  JSP,

Differences between JSP and HTML

shape Table

HTML JSP
HTML is use to develope static pages JSP is a technology to develop web applications.
HTML needs html interpreter for execution JSP needs JSP page compiler and JSP container for execution
HTML program generates static pages. Exception thrown by typeid function.
bad_function_call JSP program can generate both static and dynamic pages.
HTML is used for both java and non-java web applications. JSP is used only for java web applications

Summary

shape Key Points

  • JSP pages are converted into servlet pages.
  • Web container contains both JSP and Servlet containers.
  • Less code will be written in JSP compared to servlet that is the main advantage of JSP.
  • jspinit() will be called only once.