COMP201 Java Programming Part III: Advanced Features Topic 16: JavaServer Pages (JSP) Servlets and JavaServer Pages (JSP) 1.0: A Tutorial

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.
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
 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.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
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,
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 Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
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.
JSP Java Server Pages Softsmith Infotech.
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.
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.
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.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
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.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
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.
Java Server Pages (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets.
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)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Chapter 11 Invoking Java Code with JSP Scripting Elements.
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
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.
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.
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 -by Rubeena Memon Deepti Jain Jaya Thakar Jisha Vettuventra.
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 Java Server Pages. Hello, !
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
JSP java server pages.
Developing JavaServer Pages
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
Pre-assessment Questions
Invoking Java Code from JSP
Java Server Pages JSP 11/11/2018.
Introduction to Java Bean
Java Server Pages B.Ramamurthy.
CS320 Web and Internet Programming Java Beans
COSC 2956 Internet Tools Java Server Pages.
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.
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

COMP201 Java Programming Part III: Advanced Features Topic 16: JavaServer Pages (JSP) Servlets and JavaServer Pages (JSP) 1.0: A Tutorial

COMP201 Topic 16 / Slide 2 Objective and Outline l Objective: n Introduction to JSP l Outline: n Introduction and overview n JSP constructs –JSP scripting elements –JSP directives –JSP actions n JSP and Beans –Beans –Using beans in JSP

COMP201 Topic 16 / Slide 3 Introduction and Overview l Server-side java: n Scheme 1: –HTML files placed at location for web pages –Servlets placed at special location for servlets –Call servlets from HTML files n Scheme 2: –JSP: HTML + servlet codes + jsp tags –Placed at location for web pages –Converted to normal servlets when first accessed l Might take some time l Good idea for programmer to access it for the first time

COMP201 Topic 16 / Slide 4 l Example: Hello.jsp JSP Test JSP Test Time: n JSP Test : normal HTML n : special JSP tags n new java.util.Date(): java code Placed at: tomcat /webapps/ROOT/jsp n Accessed through web: n Also try: Introduction and Overview

COMP201 Topic 16 / Slide 5 l Ingredients of a JSP n Regular HTML –Simply "passed through" to the client by the servlet created to handle the page. n JSP constructs –Scripting elements let you specify Java code that will become part of the resultant servlet, –Directives let you control the overall structure of the servlet, and –Actions let you specify existing components that should be used, and control the behavior of the JSP engine l JavaBeans: a type of components frequently used in JSP Introduction and Overview

COMP201 Topic 16 / Slide 6 Outline l Outline: n Introduction and overview n JSP constructs –JSP scripting elements –JSP directives –JSP actions n JSP and Beans –Beans –Using beans in JSP

COMP201 Topic 16 / Slide 7 JSP Scripting Elements l JSP converted to Servlet at first access l JSP scripting elements let you insert Java codes into the servlet n Expressions: – Form – Evaluated and inserted into the output n Scriptlets –Form –Inserted into the servlet's service method n Declarations: –Form –Inserted into the body

COMP201 Topic 16 / Slide 8 l JSP Expressions: Form: n Example Time: n Processing –Evaluated, converted to a string, and inserted in the page. –At run-time (when the page is requested) JSP Scripting Elements

COMP201 Topic 16 / Slide 9 l JSP Expressions: n Several variables predefined to simply jsp expressions –request, the HttpServletRequest; –response, the HttpServletResponse; –session, the HttpSession associated with the request (if any); –out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client. n Example: Your hostname: JSP Scripting Elements Script.jsp

COMP201 Topic 16 / Slide 10 l JSP Scriptlets Form: n Example: <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %> n Inserted into the servlet's service method EXACTLY as written n Can access the same predefined variables as JSP expressions JSP Scripting Elements Script.jsp

COMP201 Topic 16 / Slide 11 l JSP Declarations: Form: Example: n Inserted into the main body of the servlet class (outside of the service method processing the request) n Normally used in conjunction with JSP expressions or scriptlets. Accesses to page since server reboot: JSP Scripting Elements Script.jsp

COMP201 Topic 16 / Slide 12 Outline l Outline: n Introduction and overview n JSP constructs –JSP scripting elements –JSP directives –JSP actions n JSP and Beans –Beans –Using beans in JSP

COMP201 Topic 16 / Slide 13 JSP Directives l Affect the overall structure of the servlet class. Form: directive attribute1="value1" attribute2="value2"... AttributeN="valueN" %> l Two commonly used types of directives n Page directives n Include directives

COMP201 Topic 16 / Slide 14 l Examples of Page directives n n –Same as : n n Try: Remove the page directive in Script.jsp and see what happens JSP Directives

COMP201 Topic 16 / Slide 15 l Include Directive n lets you include files at the time the JSP page is translated into a servlet (static including). Form: n Example situation where it is useful: –Same navigation bar at bottom of many pages. –Usage l Keep content of the navigation bar in an URL l Include the URL in all the pages JSP Directives

COMP201 Topic 16 / Slide 16 Outline l Outline: n Introduction and overview n JSP constructs –JSP scripting elements –JSP directives –JSP actions n JSP and Beans –Beans –Using beans in JSP

COMP201 Topic 16 / Slide 17 JSP Actions l JSP actions control the behavior of the servlet engine. Let one n Dynamically insert a file n Forward the user to another page n Reuse JavaBeans components n..

COMP201 Topic 16 / Slide 18 JSP Actions l The include action n Form: n Inserts the file at the time the page is requested. –Differs from the include directive, which inserts file at the time the JSP page is translated into a servlet. n Example: IncludeAction.jsp

COMP201 Topic 16 / Slide 19 JSP Actions l The forward action: n Form: " /> n Forward to the page specified. n Example: ForwardAction.jsp l Several actions related to reuse of JavaBeans components n Discuss next

COMP201 Topic 16 / Slide 20 Outline l Outline: n Introduction and overview n JSP constructs –JSP scripting elements –JSP directives –JSP actions n JSP and Beans –Beans –Using beans in JSP

COMP201 Topic 16 / Slide 21 JSP and JavaBeans l Beans n Objects of Java classes that follow a set of simple naming and design conventions –Outlined by the JavaBeans specification n Beans are Java objects –Other classes can access them and their methods –One can access them from jsp using scripting elements. n Beans are special Java objects –Can be accessed using JSP actions. –Can be manipulated in a builder tool –Why interesting? l Programmers provide beans and documentations l Users do not have to know Java well to use the beans.

COMP201 Topic 16 / Slide 22 JSP and JavaBeans l Naming conventions: n Class name: –Often include the word Bean in class name, such as UserBean n Constructor: –Must implement a constructor that takes no arguments l Note that if no constructor is provided, a default no-argument constructor will be provided.

COMP201 Topic 16 / Slide 23 l Naming conventions: Methods n Semantically, a bean consists of a collection of properties (plus some other methods) n The signature for property access (getter and setter) methods public void setPropertyName(PropertyType value); public PropertyType getPropertyName() n Example: –Property called ran k: public void setRank(String rank); public String getRank(); –Property called age : public void setAge(int age); public int getAge(); JSP and JavaBeans

COMP201 Topic 16 / Slide 24 l Properties should not be confused with instance variables n Even though instance variables are often mapped directly to property names, n Properties of a Bean are not required to correspond directly with instance variables. n Example: public class DiceBean { private java.util.Random rand; public DiceBean() { rand = new Random(); } public int getDiceRoll() { // return a number between 1 and 6 return rand.nextInt(6) + 1; } public void setDiceRoll(int i) { // do nothing } } JSP and JavaBeans

COMP201 Topic 16 / Slide 25 l Property name conventions nBegin with a lowercase letter nUppercase the first letter of each word, except the first one, in the property name. Examples: firstName, lastName lCorresponding setter and getter methods: nsetFirstName, setLastName ngetFirstName, getLastName Note the case difference between the property names and their access method JSP and JavaBeans

COMP201 Topic 16 / Slide 26 l Indexed properties n Properties whose values are sets n Example: contacts --- set of all people in contact n Conventions: public PropertyType[] getProperty() public PropertyType getProperty(int index) public void setProperty(int index, PropertyType value) public void setProperty(PropertyType[]) public int getPropertySize() JSP and JavaBeans

COMP201 Topic 16 / Slide 27 l Bean with indexed properties import java.util.*; public class StatBean { private double[] numbers; public StatBean() {numbers = new double[0]; } public double getAverage() {..} public double getSum() {.. } public double[] getNumbers() {return numbers; } public double getNumbers(int index) { return numbers[index]; } public void setNumbers(double[] numbers) { this.numbers = numbers; } public void setNumbers(int index, double value) { numbers[index] = value; } public int getNumbersSize() { return numbers.length; } } JSP and JavaBeans

COMP201 Topic 16 / Slide 28 l Boolean Properties n Properties that are either true or false n Setter/getter methods conventions public boolean isProperty(); public void setProperty(boolean b); public boolean isEnabled(); public void setEnabled(boolean b); public boolean isAuthorized(); public void setAuthorized(boolean b); JSP and JavaBeans

COMP201 Topic 16 / Slide 29 Outline l Outline: n Introduction and overview n JSP constructs –JSP scripting elements –JSP directives –JSP actions n JSP and Beans –Beans –Using beans in JSP

COMP201 Topic 16 / Slide 30 Using Beans in JSP l JSP actions for using beans: jsp:useBean –Find or instantiate a JavaBean. jsp:setProperty –Set the property of a JavaBean. –Call a setter method jsp:getProperty – Get the property of a JavaBean into the output. –Call a getter method

COMP201 Topic 16 / Slide 31 l Example: The bean package jspBean201; public class SimpleBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } } l Compile with javac and place in regular classpath n In Tomcat, same location as servlets. (can be different on other web servers) Using Beans in JSP

COMP201 Topic 16 / Slide 32 l Use SimpleBean in jsp: ReuseBean.jsp n Find and instantiate bean n Set property n Get property: call the getMessage method and insert what it returns to web page Message: Using Beans in JSP

COMP201 Topic 16 / Slide 33 l The jsp:useBean action: n Format –Simple format: –Container format: body portion executed only when bean first instantiated Body Using Beans in JSP

COMP201 Topic 16 / Slide 34 l The jsp:useBean action: n Attributes: –Scope: Indicates the context in which the bean should be made available l page (default): available only in current page l request, available only to current request l session, available only during the life of the current HttpSession l Application, available to all pages that share the same ServletContext –id: Gives a name to the variable that will reference the bean l New bean not instantiated if previous bean with same id and scope exists. –class: Designates the full package name of the bean. –type and beanName: can be used to replace the class attribute Using Beans in JSP

COMP201 Topic 16 / Slide 35 l The jsp:setProperty action: n Forms: n If the value attribute is used –String values are automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character n If the param attribute is used –No conversion Using Beans in JSP

COMP201 Topic 16 / Slide 36 l Calendar n Allow user to keep track of appointments online –login.html: for login –JSP papes: under tomcat/webapps/examples/jsp/cal l cal1.jsp: jsp for visualize appointments l cal2.jsp: jsp for editing appointments –Beans & classes: under tomcat/webapps/examples/WEB- INF/classes/cal (cal is a package) l TableBean TableBean l Entries Entries l Entry Entry Using Beans in JSP/Example

COMP201 Topic 16 / Slide 37 l cal1.jsp n Called from: –login.html: l Parameters: “name”, “ ” –cal1.jsp l Parameters: “date”, with possible values “prev” or “next” l Note: The following is never activated … –cal2.jsp l Parameters: “date”, “time”, “description” Using Beans in JSP/Example

COMP201 Topic 16 / Slide 38 l cal1.jsp n It handles requests using TableBean <% table.processRequest(request); if (table.getProcessError() == false) { %> Using Beans in JSP/Example

COMP201 Topic 16 / Slide 39 l cal1.jsp n How does TableBean handle requests? public class TableBean { JspCalendar JspCal; Entries entries; String date; String name = null; String = null; boolean processError = false; public void processRequest (HttpServletRequest request) { // Request from login.html // Get the name and . this.processError = false; if (name == null) setName(request.getParameter ("name")); if ( == null) set (request.getParameter (" ")); if (name == null || == null) { this.processError = true; return; } Using Beans in JSP/Example

COMP201 Topic 16 / Slide 40 Using Beans in JSP/Example l cal1.jsp n How does TableBean handle requests? // Request from cal1.jsp // Get the date. String dateR = request.getParameter ("date"); if (dateR == null) date = JspCal.getCurrentDate (); else if (dateR.equalsIgnoreCase("next")) date = JspCal.getNextDate (); else if (dateR.equalsIgnoreCase("prev")) date = JspCal.getPrevDate ();

COMP201 Topic 16 / Slide 41 Using Beans in JSP/Example l cal1.jsp n How does TableBean handle requests? // Request from cal2.jsp // make sure entry exist entries = (Entries) table.get (date); if (entries == null) { entries = new Entries (); table.put (date, entries); } // If time is provided add the event. String time = request.getParameter("time"); if (time != null) entries.processRequest (request, time ); } //Entry update done in the Entries class.

COMP201 Topic 16 / Slide 42 l cal2.jsp n Called from –Cal1.jsp l Parameters: “time” –It uses the parameter to call cal1.jsp l The call uses “date”, “time”, “description” l “date” from the TableBean l “description” from form (user input) Using Beans in JSP/Example