CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
Michael Brockway Application Integration JavaServer Pages l Introduction & Overview l Implicit Objects l Scripting l Standard Actions l Directives l References.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
Java Server Pages by Jon Pearce. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Introduction to JavaServer Pages Vijayan Sugumaran Dept. of DIS Oakland University Parts of this presentation was provided by
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
1 CS6320 – JSP L. Grewe 2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
© Yaron Kanza Server-Side Programming using Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
1 JSP – Java Server Pages Representation and Management of Data on the Internet.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
Java Server Pages. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
JSP Java Server Pages Softsmith Infotech.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Jordan Anastasiade. All rights reserved.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
1 JSP – Java Server Page DBI – Representation and Management of Data on the Internet.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
1 Processing phase Translation phase JSP page translation and processing phases Client Server Request Response Hello.jsp helloServlet.class helloServlet.java.
Java Server Pages Introduction. Servlet Drawbacks Web page designer will need to know servlets to design the page. Servlet will have to be compiled for.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
Web Technologies Java Beans & JSP By Praveen Kumar G.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
JSP – Java Server Page DBI – Representation and Management of Data on the Internet.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
SKT-SSU IT Training Center Servlet and JSP. Chapter Eleven: Invoking Java Code with JSP Scripting Elements.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
JSP Java Server Pages. Hello, !
Developing JavaServer Pages
Pre-assessment Questions
Introduction to JSP Java Server Pages
Knowledge Byte In this section, you will learn about:
Invoking Java Code from JSP
COP 4610L: Applications in the Enterprise Spring 2005
JSP implicit objects & directive elements
COSC 2956 Internet Tools Java Server Pages.
Presentation transcript:

CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics

The JSP Framework  JavaServer Pages (JSP) is a Java technology that allows Java code and certain pre-defined actions to be embedded into static content.  JSPs are compiled into Java Servlets by a JSP compiler.  Architecturally, JSP can be viewed as a high-level abstraction of servlets that is implemented as an extension of the Servlet API. Ref: Wikipedia

<% String param = request.getParameter("ParamName"); %> My first JSP page Parameter value is A JSP file interweaved with HTML codes and JSP scriplets

JSP vs. Servlet JSP (corresponding class)Servlet jspInit()init() jspDestroy()destroy() _jspService(request, response) service(request, response)  A JavaServer page can be viewed a Java class written in "different language".  A JavaServer page is first compiled into a Servlet class and then into Java byte code.  It has the same lifecycle as a servlet (init, serve, destroy) Static contents,, and elements in a JSP Page are translated into Java codes and placed in _jspService()

JSP Syntax  A JavaServer Page may be broken down into the following pieces:  Static data such as HTML  JSP scripting elements and variables  JSP actions  JSP directives  Custom tags with correct library

Predefined Variables (Implicit Objects)  These objects are automatically made available in a JSP page (they are declared as local variables in _jspService() )  request  The HttpServletRequest object  response  The HttpServletResponse object  out  The stream (of type JspWriter ) used to send output to the client  application  An instance of ServletContext. It is the object obtained by invoking getServletContext(). We can use this object to share data among all servlets and JSP pages in the same application.

Predefined Variables (Implicit Objects)  session  The HttpSession object associated with the request (unless disabled with the session attribute of the page directive)  page  The servlet itself (for self reference).  pageContext  A PageContext instance that contains data associated with the whole page. PageContext  config  An instance of ServletConfig. It is the object obtained by calling getServletConfig().  exception  Represent an exception not caught by the application code in an "Error handling JSP page".

Implicit objects and their corresponding class  application: javax.servlet.ServletContext  config: javax.servlet.ServletConfig  exception: java.lang.Throwable  out: javax.servlet.jsp.JspWriter  page: java.lang.Object  pageContext: javax.servlet.jsp.PageContext  request: javax.servlet.ServletRequest  response: javax.servlet.ServletResponse  session: javax.servlet.http.HttpSession

JSP Elements  Three types of JSP elements:  Directive elements  Scripting elements  Action elements  JSP Elements have two forms:  the XML form  the alternative form

Comments  Comments  Discarded by JSP container  Won't appear in the output  Treated as template data.  Reproduced in the output  Note: --> is produced as

Scripting Elements  Allow you to insert Java code in JSP pages.  Three types:  Expressions  Scriplets  Declarations

Expressions  Format:  Result  The element is replaced by the evaluated result of the expression in the output.  Examples  Current time:  Your hostname:  3 * =  XML-compatible syntax  Java Expression

Scriptlets  Format:  Result  Code is inserted verbatim into servlet's _jspService()  Examples   XML-compatible syntax  Java Code

JSP/Servlet Correspondence  Scriptlets in JSP:  Possible resulting servlet code: public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Initialization code... out.println(foo()); bar();... } No ';' after foo() as it represents an expression and not a statement

JSP Scriptlets Example <% for (int i=100; i>=0; i--) { %> bottles of beer on the wall. <% } %> for (int i=100; i>=0; i--) { out.println(i); out.println(" bottles of beer on the wall. "); } 100 bottles of beer on the wall. 99 bottles of beer on the wall. 98 bottles of beer on the wall. 97 bottles of beer on the wall. 96 bottles of beer on the wall. 95 bottles of beer on the wall. 94 bottles of beer on the wall. … JSP Resulting Servlet Output appears in browser

Example Using JSP Scriptlets Color Testing <% String bgColor = request.getParameter("bgColor"); boolean hasExplicitColor; if (bgColor != null) hasExplicitColor = true; else { hasExplicitColor = false; bgColor = "WHITE"; } %> "> …

Declarations  Format:  Result  Code is inserted verbatim into servlet's class definition, outside of any existing methods  Use this to introduce instance/static variables and methods  Examples   XML-compatible syntax  Java Code

 Original JSP Some Heading <%! private String randomHeading() { return(" " + Math.random() + " "); } %>  Possible resulting servlet code public class xxxx implements HttpJspPage { private String randomHeading() { return(" " + Math.random() + " "); } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... out.println(" Some Heading "); out.println(randomHeading());... } JSP/Servlet Correspondence

 Original JSP  Possible resulting servlet code public class xxxx implements HttpJspPage { int age1 = 100; public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... int age2 = 100;... } JSP/Servlet Correspondence (Declaration vs. Sniplet)

Example: JSP Tags + HTML Tags Table of Square Roots Number Square Root <% for (int n=0; n<=100; n++) { %> <% } %>

 References  Wikipedia: JavaServer Pages  Free Tutorial (Java, JSP, Java Servlets)  Sample JSP codes