1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.

Slides:



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

JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
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.
Web programming for project students Dr Jim Briggs.
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.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
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.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
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.
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.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
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.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
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.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
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.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
 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,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
Chapter 14 Using JavaBeans Components in JSP Documents.
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 Directive and Objects. JSP Directives As discussed before There are 3 types of elements in JSP Directive Elements Scripting Elements Standard Action.
Developing JavaServer Pages
Java Server Pages.
Knowledge Byte In this section, you will learn about:
Chengyu Sun California State University, Los Angeles
Invoking Java Code from JSP
JSP Directives 1-Jan-19.
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Web Technologies Java Beans & JSP
Presentation transcript:

1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller and Schwartzbach, 2006, Chapter 9) David Meredith

2 Scripting languages Scripting languages, like ASP and PHP, view Web applications as collections of active pages – An active page is an HTML document with embedded fragments of script code that are executed on the server Scripting languages are OK for simple applications – but weaknesses become apparent when try to use scripting languages to build large, complex Web applications e.g., lack of static type checking (i.e., checking the types of parameters and variables at compile time)

3 The JSP framework Pages written using the JavaServer Pages (JSP) scripting language are translated into servlets and then compiled using a Java compiler – implies full power of Java language (including, e.g., static type checking) is preserved This is an advantage of JSP over, e.g., ASP or PHP

4 A simple JSP example JSP page above maintains a hit counter and prints the current server time When using a Tomcat server, needs to be saved in a directory underneath webapps, e.g., $CATALINA_HOME/webapps/myjsps/hello.jsp – JSP pages compiled into servlets automatically JSP page is an HTML page with embedded Java code – variable out declared implicitly to hold current output stream JSP should be as simple as HTML but, without tag libraries or expression language, need a good knowledge of Java

5 Templates JSP page written as a template – Template is a text file (usually an HTML file) containing snippets of Java code and JSP-specific directives – Java code in a JSP page appears as expressions: – e.g., statements: – e.g., declarations: – e.g.,

6 Implicitly declared variables JSP page translated into a servlet and following variables are implicitly declared: HttpServletRequest request; HttpServletResponse response; HttpSession session; ServletContext application; ServletConfig config; Output stream declared as JspWriter out; – Unlike PrintWriter JspWriter throws a java.io.IOException if print method fails JspWriter may buffer output before printing – Constant parts of a JSP page are also printed to out PageContext pageContext; – adds extra layer of scope and corresponding setAttribute and getAttribute methods Four successively broader layers of scope for setting and getting attributes: – page scope: pageContext – request scope: request – session scope: session – application scope: application pageContext.findAttribute() – searches for named attribute in successively broader scopes

7 Expressions Java expressions are embedded in JSP pages using syntax:

8 Statements Java statements are embedded in JSP pages using syntax: Statement must have side-effects to be noticeable, e.g., – change state of variables – print to output stream

9 Declarations Java declarations are embedded in a JSP file using the syntax Can be used to define class fields and methods

10 Declaring variables in declarations and statements If line 6 were a statement, then variable hits would be set to zero every time the page was loaded – statement would correspond to a line in a method in the servlet generated from the page Using a declaration means that hits is declared as a field in the servlet generated by the page – therefore only set to zero once, when the servlet class is instantiated

11 Directives JSP directives have the syntax Directives provide parameters to the JSP processor, e.g., – includes the file header.jsp at the position where the directive occurs Note how we declare title to be a field so that we can refer to it in an expression in header.jsp

12 The page directive page directive can be used with various attributes to set different properties of the JSP page – buffer="size" sets size of output stream buffer in bytes – autoFlush = "true-of-false" determines whether output buffer is automatically flushed – contentType = "mime-type" sets mime type for output stream – default is "text/html" – pageEncoding = "encoding" sets character set for output – default is ISO – info="string" sets descriptive string returned by invoking getServletInfo() on generated servlet – errorPage="path" sets JSP page that should be invoked if uncaught exception thrown – isErrorPage="true-or-false" determines whether current page is an error page – if it is, then there is a variable, exception, available that contains the thrown exception – import="package" specifies package to be imported in generated servlet class

13 Using page directive to control error handling

14 Translation into servlets Tomcat generates servlet that specializes HttpJspBase (a subclass of Servlet) Expressions wrapped in out.print(...) HTML markup wrapped in out.write(...) Declarations translated into fields and methods

15 Translation into servlets Translation is lexical – means that HTML markup and Java code are not parsed – means Java code statements don't have to be individually syntactically well-formed - only result that has to be well-formed

16 XML version of JSP JSP processor performs lexical translation of JSP pages into servlets – means no syntactic requirements are enforced JSP can also be written using an XML syntax – called JSP documents (not pages) – means JSP documents can be validated – can be manipulated using XML tools

17 Example JSP document

18 JSP Expression Language ${expression} occurring anywhere within JSP template text or attribute values of markup is replaced with string resulting from evaluating expression Expression language resembles JavaScript Supports – strings, booleans, various numerical types – arithmetic, logical and comparison operators References to named variables resolved using findAttribute mechanism Supports operations on certain objects, e.g., ${gadget.weight} is translated into pageContext.findAttribute("gadget").getWeight() Some implicit objects defined, e.g., – param is a map of the request parameters – pageContext references the object pageContext

19 Tags JSP is intended to be used by people who are not expert Java programmers However, hard to use JSP without knowledge of Java Tags designed to allow markup to be separated cleanly from Java code – Standard Tag Library can be used by non-programmers to access JSP through mark-up

20 Tag files A tag file is a definition of a new tag (i.e., element) which can contain arbitrary Java code to be executed Means the active content can be obtained by designer simply by using appropriate tags in the markup Tag file is a JSP page with the file extension.tag and the directive Note use of attribute directive to declare an attribute for the new tag jsp:doBody instruction indicates where contents of tag is inserted taglib directive indicates where tags are located (cf. XML namespaces)

21 Using jsp:doBody with the var attribute Declare a variable in the var attribute of the jsp:doBody instruction Content of new tag is stored in the declared variable Each reference to declared variable in tag file then replaced with content of new tag when used