2. • JSP technology is used to create web application
just like Servlet technology. It can be thought of
as an extension to Servlet because it provides
more functionality than servlet such as
expression language, etc.
• A JSP page consists of HTML tags and JSP tags.
The JSP pages are easier to maintain than Servlet
because we can separate designing and
development. It provides some additional
features such as Expression Language, Custom
Tags, etc.
3. Advantages of JSP over Servlet
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the
features of the Servlet in JSP. In addition to, we can use implicit objects,
predefined tags, expression language and Custom tags in JSP, that makes
JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business
logic with presentation logic. In Servlet technology, we mix our business
logic with the presentation logic.
3) Fast Development:
If JSP page is modified, we don't need to recompile and redeploy the
project. The Servlet code needs to be updated and recompiled if we have
to change the look and feel of the application.
4) Less code than Servlet
In JSP, we can use many tags such as action tags, custom tags, etc. that
reduces the code. Moreover, we can use implicit objects, etc.
4. The Lifecycle of a JSP Page
• Translation of JSP Page
• Compilation of JSP Page
• Classloading (the classloader loads class file)
• Instantiation (Object of the Generated Servlet is
created).
• Initialization ( the container invokes jspInit() method).
• Request processing ( the container invokes
_jspService() method).
• Destroy ( the container invokes jspDestroy() method).
• Note: jspInit(), _jspService() and jspDestroy() are the
life cycle methods of JSP.
5. • JSP page is translated into
Servlet by the help of JSP
translator.
• The JSP translator is a part of
the web server which is
responsible for translating
the JSP page into Servlet.
• After that, Servlet page is
compiled by the compiler and
gets converted into the class
file.
• Moreover, all the processes
that happen in Servlet are
performed on JSP later like
initialization, committing
response to the browser and
destroy.
6. JSP Declarations
• The JSP page that we write is turned into class
definition.
• When we declare a variable or method in jsp
inside Declaration Tag.
• We can declare static member, instance
variable and methods inside declaration Tag.
• Syntax
<%!Declaration code %>
7. JSP directives
• The jsp directives are messages that tells the web
container how to translate a JSP page into the
corresponding servlet.
• There are three types of directives:
• page directive
• include directive
• taglib directive
• Syntax of JSP Directive
• <%@ directive attribute="value" %>
8. JSP page directive
• The page directive defines attributes that
apply to an entire JSP page.
• Syntax of JSP page directive
• <%@ page attribute="value" %>
• Attributes of JSP page directive
• Import, contentType, extends,info
• Buffer, language, isThreadSafe
• autoFlush, session, pageEncoding
• errorPage, isErrorPage
9. Jsp Include Directive
• The include directive is used to include the
contents of any resource it may be jsp file, html
file or text file. The include directive includes the
original content of the included resource at page
translation time (the jsp page is translated only
once so it will be better to include static resource).
• Advantage of Include directive
• Code Reusability
• Syntax of include directive
• <%@ include file="resourceName" %>
10. JSP Taglib directive
• The JSP taglib directive is used to define a tag
library that defines many tags. We use the TLD
(Tag Library Descriptor) file to define the tags.
In the custom tag section we will use this tag
so it will be better to learn it in custom tag.
• Syntax JSP Taglib directive
• <
%@ taglib uri="uriofthetaglibrary" prefix="pre
fixoftaglibrary" %>
11. JSP expression
• The code placed within JSP expression
tag is written to the output stream of the
response. So you need not write out.print() to
write data. It is mainly used to print the values
of variable or method.
• Syntax of JSP expression tag
• <%= statement %>
12. JSP scriptlet/Snippets
• A scriptlet tag is used to execute java source
code in JSP.
• The code that appears between the <%and%>
delimiters is called snippets.
• Syntax
• <% java source code %>
13. JSP Implicit Objects
• The Implicit objects are predefined variables used to
access request and application data.
• These objects are used by the scripting elements.
14. JSP Session tracking
• In a web application, server may be responding
to several clients at a time so session tracking
is a way by which a server can identify the
client.
• As we know HTTP protocol is stateless which
means client needs to open a separate
connection every time it interacts with server
and server treats each request as a new
request.
15. Session Tracking Techniques
• There are four techniques which can be used
to identify a user session.
• a) Cookies
• b) Hidden Fields
• c) URL Rewriting
• d) Session Object
16. • Cookie
• Cookie is a key value pair of information, sent by the server to the browser
and then browser sends back this identifier to the server with every
request
• Hidden Field
• Hidden fields are similar to other input fields with the only difference is
that these fields are not displayed on the page but its value is sent as
other input fields
• URL Rewriting
• URL Rewriting is the approach in which a session (unique) identifier gets
appended with each request URL so server can identify the user session.
• Session Object
• Session object is representation of a user session. User Session starts
when a user opens a browser and sends the first request to server. Session
object is available in all the request (in entire user session) so attributes
stored in Http session in will be available in any servlet or in a jsp.