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.

Slides:



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

Java Server Pages (JSP)
 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.
Advanced Java Server Pages An more detailed look at JSPs.
JSP Tag Extensions And Java Bean. JSP Tag Extensions Tag extensions look like HTML (or rather, XML) tags embedded in a JSP page. They have a special meaning.
Java Server Pages by Jon Pearce. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
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.
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.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
1 Guide to JSP common functions 1.Including the libraries as per a Java class, e.g. not having to refer to java.util.Date 2.Accessing & using external.
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.
Basic Elements JSP For a Tutorial, see:
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.
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
® 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.
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.
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
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.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Java Server Pages (JSP)
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
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.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
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.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
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.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java and the Web CSE 3330 Southern Methodist University.
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.
Introduction To HTML Dr. Magdi AMER. HTML elements.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 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.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
JSP Java Server Pages. Hello, !
Developing JavaServer Pages
Scripted Page Web App Development (Java Server Pages)
Pre-assessment Questions
Java Server Pages.
Presentation transcript:

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 List and describe the JSP element types Explain the operation of a JSP error page Explain the operation of include and forward directives Explain the purpose of a Java Bean and how one can be used from a JSP Explain the purpose of a taglib and how a tag can be used from a JSP

Separation of Concerns A basic principle of software design: Software modules should have distinct responsibilities as much as possible input calculation data retrieval output

Separation of Content From Processing (1/2) A servlet generating an HTML document intermixes Java code and HTML public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException() { PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println(" " + "Example HTML" + " ";... out.println(" "); out.close(); }

Separation of Content From Processing (2/2) Java Server Pages (JSP) allow static HTML content to be separated from dynamic content (created by Java code) within a document Example Page Example Page Hello,...

JSP Elements JSP tags belong to one of five element types Declaration:declare variable or procedure Expression:evaluate a Java expression Scriptlet:execute a Java code segment Directive:a JSP compiler command Action Element: interacts with another component

JSP Declaration A declaration can be used to initialize variables or define procedures for use later in the JSP <%! // return the difference between two integers public int diff(int a, int b) { return (a < b) ? (b - a) : (a – b); } %>

JSP Expression A JSP expression is evaluated and then replaced by that value Today is: Generated HTML: Today is: Mon Jun 15 08:05:00 EST 2009

JSP Scriptlet A JSP scriptlet is a short section of Java code that is executed in-line Scriptlets have access to several pseudovariables: –outHTML output stream –sessioncurrent HttpSession object –requestHTTPRequest object –responseHTTPResponse object

JSP Scriptlet This scriptlet displays the current date and time <% // output the current date and time Date today = new Date(); out.println(" Today is: " + date.toString() + " "; %>

JSP Scriptlet The scriptlet checks to see if a Boolean session attributed named "login" exists <% // check to see if the user is logged in boolean loggedIn = ((Boolean) session.getAttribute("login")).booleanValue(); if (loggedIn) { out.println(" Please select an option "; } else { out.println(" Please login "; } %>

JSP Scriptlet The scriptlet maintains a "hit count" <% // get or create hit count Integer hitCount = (Integer) session.getAttribute("hits"); if (hitCount == null) { hitCount = new Integer(0): } // increment and save hit count hitCount = new Integer(hitCount.intValue() + 1); session.setAttribute("hits", hitCount); // display hit count Hit Count: %>

JSP Life-Cycle JSP's are actually executed as servlets The first time a JSP is requested, it is translated to a servlet and compiled Each subsequent request executes the same servlet JSP Servlet translated upon first request

JSP Life-Cycle

JSP Directive The page directive appears at the top of each JSP and controls its execution The page directive also specifies imports

JSP Errors Compilation errors and run-time exceptions are logged and then returned as an HTML response ???

JSP Error Page The proper way to handle errors is to declare an error page The error page is invoked whenever a JSP compilation or run-time error occurs The error page can display a standard error message

JSP Error Page JSP that might throw an error:... possible errors... stderror.jsp: Error Page That request cannot be completed at this time. Please contact your system administrator.

JSP Action Element JSP action elements control interactions with other JSP's, including –include: add the contents of another JSP –forward: send a request to another JSP for handling

JSP Include A JSP include can be used to add standard content to a JSP, such as headers and footers...unique content of this JSP...

JSP Forward A JSP forward causes control to be transferred to a different JSP the scriptlet is continued after the directive, which is not a Java statement

Java Beans A Java Bean is a Java class that can be invoked from a JSP This allows complex processing required by a JSP to be separated from the JSP A JSP can instantiate an instance of a bean class and invoke its methods

Java Bean Example Java Bean package bean; public class AdderUtility { public int add(int a, int b) { return a + b; } JSP <% int a, b, sum; … int sum = adder.add(a, b); %>

Java Bean Uses Java Beans are typically used to encapsulate complex application logic, including calculations, database access, or network access JSP Java Bean Database Network Application

JSP Taglibs A tag library allows programmers to create custom tags to invoke application functions with JSPs Each custom tag is associated with a Java class that is executed when the tag is evaluated

Taglib Example JSP code: This code appears in a green box This code appears in a green box tag body content

Taglib Example BoxInTag.java public class BoxInTag extends BodyTagSupport { private String border; public void setBorder(String border) { this.border = border; } public int doStartTag() throws JspTagException { return EVAL_BODY_BUFFERED; } public int doEndTag() throws JspTagException { JspWriter out = pageContext.getOut(); String content = bodyContent.getString(); try { out.println("<div style=\"border-style: solid; border-color: " + border + "\">" + content + " " ); } catch (IOException ioe) { ioe.printStackTrace(); } return EVAL_PAGE; } returns the tag body content, i.e., the text between the tag opening and closing

Tag Library Definition File (TLD) <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" " <taglib xmlns=" xmlns:xsi=“ xsi:schemaLocation=" version="2.0"> 1.0 Show Standings Tag boxin tags.BoxInTag Draw a box around the content tagdependent

Review Java Server Pages JSP life-cycle JSP Elements JSP include and forward directives Java Beans with JSP JSP taglibs