Top Banner
Slide 1 of 20 Introduction to JSP
20

Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech

Nov 11, 2014

Download

Technology

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 1 of 20

Introduction to JSP

Page 2: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 2 of 20

Objectives

JSP Benefits of JSP JSP Request – Response Cycle Servlet – JSP JSP Life Cycle Elements / Tags of JSP Implicit Objects

Page 3: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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

Page 4: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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

Page 5: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 5 of 20

JSP Request-Response Cycle

Page 6: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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

Page 7: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 7 of 20

JSP Life Cycle

Translation

Compilation

Execution

Page 8: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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: >

Page 9: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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.*”

Page 10: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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”

Page 11: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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” />

Page 12: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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

Page 13: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 13 of 20

Action Tags / Elements

Forward

Include

Plug-in

Bean tags

Page 14: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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>

Page 15: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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

Page 16: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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>

Page 17: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 17 of 20

Java Bean

Page 18: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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” />

Page 19: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

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

Page 20: Session 5 : intro to jsp  - Giáo trình Bách Khoa Aptech

Slide 20 of 20

Summary

Implicit Objects Action Tags

– Forward– Include– Plug-in– Bean Tags