Slide 1 of 20 Introduction to JSP
Nov 11, 2014
Slide 1 of 20
Introduction to JSP
Slide 2 of 20
Objectives
JSP Benefits of JSP JSP Request – Response Cycle Servlet – JSP JSP Life Cycle Elements / Tags of JSP Implicit Objects
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
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
Slide 5 of 20
JSP Request-Response Cycle
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
Slide 7 of 20
JSP Life Cycle
Translation
Compilation
Execution
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: >
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.*”
Slide 10 of 30
Page Directives
Attribute Default Value Examples
session “true” session=“false”
info None info=“Registration Form”
buffer “8kb” Buffer=“12kb”Buffer=“false”
autoFlush “true” autoFlush=“true”
isThreadSafe “true” isThreadSafe=“false”
errorPage None errorPage=“results/error.jsp”
isErrorPage “false” isErrorPage=“true”
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” />
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
Slide 13 of 20
Action Tags / Elements
Forward
Include
Plug-in
Bean tags
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>
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
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>
Slide 17 of 20
Java Bean
Slide 18 of 20
Bean Tags– <jsp:useBean /> action tag is used to create a
reference and include an existing bean component in JSP
– <jsp:useBean id=“myCar” class=“cars.CarBean” scope=“page | request | session | application” />
– <jsp:setProperty name=“myCar” property=“make” value=“Ford” />
– <jsp:setProperty name=“myCar” property=“make” param=“txtMake” />
– <jsp:getProperty name=“myCar” property=“make” />
Slide 19 of 20
Summary JSP Benefits of JSP JSP Request – Response Cycle JSP Life Cycle Elements / Tags of JSP
– Comments– Directives– Declarations– Expression– Scriptlet
Slide 20 of 20
Summary
Implicit Objects Action Tags
– Forward– Include– Plug-in– Bean Tags