JSP Life Cycle
A JSP life cycle is defined as the process from its creation till the destruction. This is similar
to a servlet life cycle with an additional step which is required to compile a JSP into servlet.
Steps are-
Compilation
Initialization
Execution
Cleanup
Figure. Life cycle
1. When a browser asks for a JSP, the JSP engine first checks to see whether it needs to
compile the page. If the page has never been compiled, or if the JSP has been modified
since it was last compiled, the JSP engine compiles the page. The compilation process
involves three steps −
Parsing the JSP.
Turning the JSP into a servlet.
Compiling the servlet.
2. When a container loads a JSP it invokes the jspInit() method before servicing any
requests. If you need to perform JSP-specific initialization, override the
existing jspInit() method.
3. This phase of the JSP life cycle represents all interactions with requests until the JSP is
destroyed.
4. Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP
engine invokes the _jspService() method in the JSP. The _jspService() method takes
an HttpServletRequest and an HttpServletResponse as its parameters.
5. The _jspService() method of a JSP is invoked on request basis. This is responsible for
generating the response for that request and this method is also responsible for
generating responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc.
6. The destruction phase of the JSP life cycle represents when a JSP is being removed from
use by a container. The jspDestroy() method is the JSP equivalent of the destroy
method for servlets. Override jspDestroy when you need to perform any cleanup, such as
releasing database connections or closing open files.
JSP Elements
There are three JSP elements which are used to write any JSP code in an HTML page.
1. Declaration <%! Declaration code here %>
2. Expression <%= variable/object/method call here %>
3. Scriptlet <% any java code %>
Declaration
<html>
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
<html>
Expression
<html>
<head><title>A Comment Test</title></head>
<body>
<p>Today's date: <%=([Link]())%></p>
</body>
</html>
Scriptlet
<html>
<head><title>A Comment Test</title></head>
<body>
<h2>A Test of Comments</h2>
<%for(int i=0;i<10;i++)
{
[Link](“value of counter=”+i);
}
%>
</body>
</html>
Implicit Objects
There are some objects already available in JSP. Developer not needed to create it explicitly
unlike in servlet. We can use those objects directly as per our requirement. Those objects are
known as implicit objects. Those are as follows –
request
This is the HttpServletRequest object associated with the request.
response
This is the HttpServletResponse object associated with the response to the client.
out
This is the PrintWriter object used to send output to the client.
session
This is the HttpSession object associated with the request.
application
This is the ServletContext object associated with the application context.
config
This is the ServletConfig object associated with the page.
pageContext
This encapsulates use of server-specific features like higher
performance JspWriters.
page
This is simply a synonym for this, and is used to call the methods defined by the
translated servlet class.
Exception
The Exception object allows the exception data to be accessed by designated JSP.
JSP Page directives:
Directives are used to include any package, to set any parameter related to the
jsp page, to link any other external taglib libraries. Directives indicate how the
translation of the JSP page will be? There are three different types of directives
used in any JSP page.
[Link]
2. include
3. taglib
--page directive is represented by <%@ page attribute=value%>. Here we can
set may attributes of the page i.e. import, errorPage, session, buffer, isErrorPage
etc.
-- include directive is represented by <%@ include file =”file name”%>. This
directive is used to include response of any page in the current JSP page.
-- taglib directive is represented by <%@taglib uri=” Url” prefix=”value”%>.
This is useful to include any taglib library in current JSP page. Here prefix is
used to denote the use of any tag of the library. Taglib library is used to perform
any actions in java with look and feel of html tags. It makes logic simple as
html as.