Chapter 14 Using JavaBeans Components in JSP Documents.

Slides:



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

Java Server Pages (JSP)
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.
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.
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.
Component-Based Software Engineering Internet Applications Paul Krause.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
Using JavaBeans Components in JSP Documents. Contents 1. Why using beans? 2. What are beans? 3. Using Beans: Basic Tasks 4. Sharing Beans 5. Practice.
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.
What Are Beans? beans are simply Java classes that are written in a standard format. A bean class must have a zero-argument (default) constructor. A bean.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
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,
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
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:
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
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.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
1 Processing phase Translation phase JSP page translation and processing phases Client Server Request Response Hello.jsp helloServlet.class helloServlet.java.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action 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.
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.
USING JAVABEANS COMPONENT IN JSP DOCUMENTS. References Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000.
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.
JSP in Action. JSP Standard Actions forward, include, useBean, setProperty, getProperty, text, element, and plugin Additional actions param, params,
JSP Java Server Pages. Hello, !
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
JSP Action Elements Lec - 37.
Advanced Java Programming
Java Server Pages By: Tejashri Udavant..
Scripted Page Web App Development (Java Server Pages)
USING JAVABEANS COMPONENTS IN JSP DOCUMENTS K. Phani Sirisha
Scope and State Handling in JSPs
Pre-assessment Questions
Introduction to Java Bean
Invoking Java Code from JSP
Introduction to Java Bean
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Server Pages B.Ramamurthy.
CS320 Web and Internet Programming Java Beans
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)
Web Technologies Java Beans & JSP
Presentation transcript:

Chapter 14 Using JavaBeans Components in JSP Documents

Contents 1. Why use Beans? 2. What are Beans? 3. Using Beans: Basic tasks 4. Setting Bean properties: Advanced Techniques 5. Sharing Beans 6. Practice exercises

1. Why Use Beans? Separate the classes are easier to write, compile, test, debug and reuse. Beans are merely regular Java classes that follow some simple conventions defined by the JavaBeans specification. Beans extend no particular class Beans are in no particular package, Beans use no particular interface.

1. Why Use Beans? (cont.) Visual manipulation tools and other programs can automatically discover information about classes that follow this format Can create and manipulate the classes without the user having to explicitly write any code. In JSP in particular, use of JavaBeans components provides three advantages over scriptlets and JSP expressions that refer to normal Java classes.

1. Why use Beans? (Cont.) No Java Syntax By using beans, can manipulate Java objects using only XML-compatible syntax: no parentheses, semicolons, or curly braces. Simpler object sharing. When you use the JSP bean constructs, you can much more easily share objects among multiple pages or between requests than if you use the equivalent explicit Java code.

2. What Are Beans? A bean class must have a zero-argument (default) constructor. A bean class should have no public instance variables (fields). Persistent values should be accessed through methods called getXxx and setXxx.

3. Using Beans: Basic Tasks Using three main constructs to build and manipulate JavaBeans components in JSP pages. jsp:useBean. In the simplest case, this element builds a new bean. It is normally used as follows: If you supply a scope attribute, the jsp:useBean element can either build a new bean or access a preexisting one.

jsp:getProperty This element reads and outputs the value of a bean property. Reading a property is a shorthand notation for calling a method of the form getXxx.

jsp:setProperty This element modifies a bean property.

Building Beans: jsp:useBean The jsp:useBean action lets you load a bean to be used in the JSP page. The simplest syntax:

Example The JSP action: As equivalent to the scriptlet:

4. Setting Bean Properties: Advanced Techniques Using jsp:setProperty to set bean properties takes three attributes: name (which should match the id given by jsp:useBean) property (the name of the property to change) value (the new value).

4. Setting Bean Properties: Advanced Techniques For example, the SaleEntry class shown in Listing 14.3 has: Listing 14.3 itemID property (a String), numItems property (an int), discountCode property (a double), and two read-only properties, itemCost and totalCost (each of type double). Listing 14.4 shows a JSP file that builds an instance of the SaleEntry class by means of: Listing 14.4

4. Setting Bean Properties: Advanced Techniques Listing 14.5 gives the HTML form that collects the request parameters. Listing 14.5 The results are shown in Figure 14-4.Figure 14-4

4. Setting Bean Properties: Advanced Techniques Once the bean is instantiated, using a request parameter to set the itemID is straightforward, as shown below. <jsp:setProperty name="entry" property="itemID" value=' ' />

4. Setting Bean Properties: Advanced Techniques Most JSP attribute values have to be fixed strings, but the value attribute of jsp:setProperty is permitted to be a request time expression. If the expression uses double quotes internally, In any case, the point is that it is possible to use JSP expressions here, but doing so requires the use of explicit Java code. The next two subsections will discuss how to solve these problems.

Associating Individual Properties with Input Parameters associate a property with a request parameter and automatically perform type conversion from strings to numbers, characters, and boolean values you use param to name an input parameter

Associating Individual Properties with Input Parameters (Cont.) You can simplify the code slightly if the request parameter name and the bean property name are the same. In that case, you can omit param as in the following example.

Associating All Properties with Request Parameters

Associating All Properties with Request Parameters (Cont.) Note:  No action is taken when an input parameter is missing  Automatic type conversion does not guard against illegal values as effectively as does manual type conversion  Bean property names and request parameters are case sensitive

5. Sharing Beans Up to this point,the objects that were created with jsp:useBean as though they were simply bound to local variables in the _jspService method. That is not the only behavior. They are also stored in one of four different locations, depending on the value of the optional scope attribute of jsp:useBean.

5. Sharing Beans When you use scope, the system first looks for an existing bean of the specified name in the designated location. Only when the system fails to find a preexisting bean does it create a new one. The scope attribute has four possible values: page (the default), request, session, and application.

Every page and every request has a different PageContext object, using scope="page" (or omitting scope) indicates that the bean is not shared and thus a new bean will be created for each request

This value signifies that, in addition to being bound to a local variable, the bean object should be placed in the HttpServletRequest object for the duration of the current request, where it is available by means of the getAttribute method

This value means that, in addition to being bound to a local variable, the bean will be stored in the HttpSession object associated with the current request, where it can be retrieved with getAttribute.

This value means that, in addition to being bound to a local variable, the bean will be stored in the ServletContext available through the predefined application variable or by a call to getServletContext. The ServletContext is shared by all servlets and JSP pages in the Web application. Values in the ServletContext can be retrieved with the getAttribute method.

6. Practice Exercise 1 Using Beans to develop a web application that allows student to calculate the average score. Template interface: