2. Slide 2 of 20
Objectives
JSP
Benefits of JSP
JSP Request – Response Cycle
Servlet – JSP
JSP Life Cycle
Elements / Tags of JSP
Implicit Objects
3. Slide 3 of 20
JSP
Java Server Page (JSP) is a server side script
language
Saved with .jsp extension
A simple, yet powerful Java technology for
creating and maintaining dynamic-content webs
pages
JSP page are converted by the web container
into a Servlet instance
It focus on the presentation logic of the web
application
4. Slide 4 of 20
Benefits of JSP
Segregation of the work profiles of a Web
designer and a Web developer
Emphasizing Reusable Components
Simplified Page Development
6. Slide 6 of 30
Servlet - JSP
Servlet JSP
HTML Code inside Java Java Code inside HTML
Extensive Coding Less Coding
Saved with .java Saved with .jsp
Designed for Business Logic Designed for Presentation Logic
Any changes are made to the html
code, then necessary to compile and
test the HTML code
No need to compile again and again to
obtain the required changes.
Manual Compilation is required to
changes made to any files
Automatically incorporates changes made
to any files
7. Slide 7 of 20
JSP Life Cycle
Translation
Compilation
Execution
8. Slide 8 of 30
Elements / Tags of JSP
Types of Tags
– Scripting-oriented tags
– XML-based tags
Elements / Tags Syntax
Comments <%-- --%>
Directives <%@ %>
Declarations <%! %>
Expression <%= %>
Scriptlet <% %>
Actions <jsp: />, <jsp: > </jsp: >
9. Slide 9 of 30
Page Directives
Defines attributes that notify the JSP engine about the
general settings of a JSP page
– <%@page attribute1=“value” attribute2=“value” %>
– <jsp:directive.page attribute1=“value” attribute2=“value” />
Attribute Default Value Examples
language “java” “java”
contentType “text/htm” contentType=“text/html”
contentType=“text/xml”
extends None extends=“com.Login”
import None import=“java.util.Vector,
java.sql.*”
11. Slide 11 of 20
Include / Taglib Directory
Include directory is used to include contents of
one file in another.
– <%@include file=“footer.jsp” %>
– <jsp:directive.include file=“footer.jsp” />
Taglib directory is used custom tag
– <%@taglib uri=“ATMTag.tld” prefix=“atm” %>
– <jsp:directive.taglib uri=“ATMTag.tld”
prefix=“atm” />
12. Slide 12 of 30
Implicit Objects
Object Class / Interface
page javax.servlet.jsp.HttpJspPage
config javax.servlet.ServletConfig
request javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
out javax.servlet.jsp.JspWriter
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
pageContext javax.servlet.jsp.PageContext
exception java.lang.Throwable
13. Slide 13 of 20
Action Tags / Elements
Forward
Include
Plug-in
Bean tags
14. Slide 14 of 20
Forward
To permanently transfer control from a JSP page
to another location on the local server
– <jsp:forward page=“/user/UserHome.jsp” />
– <jsp:forward page=“/user/ChangePassword.jsp”>
• <jsp:param name=“UserId” value=“common” />
– </jsp:forward>
15. Slide 15 of 20
Include
To incorporate the content or insert a file from
another page into current page
– <jsp:include page=“ChangePwd.jsp” flush=“true” />
– <jsp:include page=“ChangePwd.jsp” flush=“true”>
<jsp:param name=“UserId” value=“common” />
– </jsp:include>
The include directive inserts the file at the time
the JSP page is translated into a Servlet
The include action inserts the file at the time
the page is requested
16. Slide 16 of 20
Plug-in
– The <jsp:plugin> action is used to generate browser-
specific HTML for specifying Java applets which rely
on Sun Microsystem’s Java plug-in
<jsp:plugin type=“applet” code=“test.AppletTest”
codebase=“/classes/applets” height=“150” width=“100”>
<jsp:params>
<jsp:param name=“color” value=“blue” />
<jsp:param name=“speed” value=“fast” />
</jsp:params>
<jsp:fallback> Your browser can’t display this applet text </jsp:fallback>
</jsp:plugin>