31-Module 7 - JSP - JSP Tags and Expressions-01-Apr-2019Reference Material I - JSP
31-Module 7 - JSP - JSP Tags and Expressions-01-Apr-2019Reference Material I - JSP
Pages)
Servlet vs JSP
Like JSP, Servlets are also used for generating dynamic webpages.
• Servlets
• Servlets are Java programs which supports HTML tags too.
• Generally used for developing business layer of an enterprise
application.
• Servlets are created and maintained by Java developers.
• JSP –
• JSP program is a HTML code which supports java statements too.
• Used for developing presentation layer of an enterprise application
• Frequently used for designing websites and used for web
designers.
Overview
• Java Server Pages (JSP) is a server side technology for
developing dynamic web pages.
• This is mainly used for implementing presentation layer (GUI
Part) of an application.
• A complete JSP code is more like a HTML with bits of java
code in it.
• JSP is an extension of servlets and every JSP page first gets
converted into servlet by JSP container before processing the
client’s request.
Dynamic web pages
• Dynamic webpages can have two types of contents – static &
dynamic content.
• All JSP programs are stored as a .jsp files. Above is a simple JSP
code, MyJSP.jsp which prints Hello, Sample JSP code. As
discussed JSP is used for creating dynamic webpages.
Life Cycle of JSP page
• Compilation
• Initialization
• Execution
• Cleanup
JSP Compilation:
• 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.(.jsp to .java)
• Turning the JSP into a servlet.
• Compiling the servlet. Page Translation (.java to .class)
JSP Initialization:
• 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
jspInit() method:
public void jspInit()
{
// Initialization code...
}
• Typically initialization is performed only once and as with the
servlet init method, you generally initialize database
connections, open files, and create lookup tables in the
jspInit method.
JSP Execution
• This phase of the JSP life cycle represents all interactions with
requests until the JSP is destroyed.
• 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 as follows:
void _jspService(HttpServletRequest request,
HttpServletResponse response)
{ // Service handling code... }
• The _jspService() method of a JSP is invoked once per a request
and 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 ie. GET, POST, DELETE etc.
JSP Cleanup:
• 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.
• The jspDestroy() method has the following form:
public void jspDestroy()
{
// Your cleanup code goes here.
}
JSP elements
• Directives of the form <%@ ... %>
• Scripting elements
• Expressions of the form <%= expr %>
• Scriptlets of the form <% code %>
• Declarations of the form <%! code %>
• JSP Comments <%-- ... --%>
• Implicit objects.
• Standard actions
• Example: <jsp:useBean> ... </jsp:useBean>
1. JSP Directives
• Directives control the processing of an entire JSP page. It gives
directions to the server regarding processing of a page.
• Syntax of Directives: