Introduction to Java Bean

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Road Map Introduction to object oriented programming. Classes
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
DT228/3 Web Development Java Beans. Intro A major problem with JSP is tendency to mix java code with HTML  -  web designer write the HTML and programmer.
COMP201 Java Programming Part III: Advanced Features Topic 16: JavaServer Pages (JSP) Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
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 server pages is a technology for developing portable java web applications that runs on a java web server. Main motivation behind developing JSP was.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
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.
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 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.
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
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 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
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 Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
CourseOutline Example & JavaBeans Lec Start with Example Displaying Course Outlines User will select either course “web design & development” or.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
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.
JSP in Action. JSP Standard Actions forward, include, useBean, setProperty, getProperty, text, element, and plugin Additional actions param, params,
Chapter 14 Using JavaBeans Components in JSP Documents.
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.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
JSP Action Elements Lec - 37.
JSP java server pages.
JSP Based on
Examples of Classes & Objects
Java Server Pages By: Tejashri Udavant..
COMP9321 Web Application Engineering Semester 2, 2017
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
Chapter 3: Using Methods, Classes, and Objects
Pre-assessment Questions
Introduction to JSP Java Server Pages
JavaBeans and JSP CS-422.
Java Server Pages JSP 11/11/2018.
Introduction to Java Server Pages
Introduction to Java Bean
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 9 Objects and Classes
Java Server Pages (JSP)
Java Server Pages B.Ramamurthy.
CS320 Web and Internet Programming Java Beans
Scope and State Handling in Java Server Pages
Chapter 11 Inheritance and Polymorphism Part 1
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
Chapter 5 Classes.
COSC 2956 Internet Tools Java Server Pages.
Presentation transcript:

Introduction to Java Bean

Remember: JSP Standard Actions Standard actions are well known tags that affect the run time behavior of the JSP and the response sent back to the client. Some commonly used tag actions types are: <jsp:useBean> <jsp:setProperty> <jsp:getProperty> <jsp:param> <jsp:include> <jsp:forward> <jsp:plugin>

Java Bean A Java Bean is a java class that should follow following conventions: It should have a no-arg constructor It should be Serializable It should provide methods to set and get the values of the properties, known as getter and setter methods Why use Java Bean? it is a reusable software component A bean encapsulates many objects into one object, so we can access this object from multiple places. Moreover, it provides the easy maintenance.

Simple example of java bean class //Employee.java   package mypack;   public class Employee implements java.io.Serializable{   private int id;   private String name;      public Employee(){}   public void setId(int id){this.id=id;}   public int getId(){return id;}   public void setName(String name){this.name=name;}   public String getName(){return name;}   }  //To access the java bean class, we should use getter and setter methods. package mypack;   public class Test{   public static void main(String args[]){      Employee e=new Employee();//object is created   e.setName("Arjun");//setting value to the object   System.out.println(e.getName());   }}  

jsp:useBean action tag The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean. Syntax <jsp:useBean id= "instanceName" scope= "page | request | session | application"    class= "packageName.className" type= "packageName.className"   beanName="packageName.className | <%= expression >" >   </jsp:useBean> 

Attributes and Usage id: is used to identify the bean in the specified scope. scope: represents the scope of the bean. It may be page, request, session or application. The default scope is page. page: specifies that you can use this bean within the JSP page. The default scope is page. request: specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page. session: specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It has wider scope than request. application: specifies that you can use this bean from any JSP page in the same application. It has wider scope than session. class: instantiates the specified bean class (i.e. creates an object of the bean class) but it must have no-arg or no constructor and must not be abstract. type: provides the bean a data type if the bean already exists in the scope. It is mainly used with class or beanName attribute. If you use it without class or beanName, no bean is instantiated. beanName: instantiates the bean using the java.beans.Beans.instantiate() method.

Simple example of jsp:useBean action tag Calculator.java package com.javatpoint; public class Calculator{ public int cube(int n){return n*n*n;} } index.jsp <jsp:useBean id="obj" class="com.javatpoint.Calculator"/>      <%   int m=obj.cube(5);   out.print("cube of 5 is "+m);   %>  

Javabeans A Javabeans is a special type of the class that has a number of methods. The JSP page can call these methods so can leave most of the code in these Javabeans. For example, if you wanted to make a feedback form that automatically sent out an email. By having a JSP page with a form, when the visitors presses the submit button this sends the details to a JavaBeans that sends out the emails. This way there would be no code in the JSP page dealing with sending emails. To use a Javabean in a JSP page use the following syntax: <jsp:usebean id=“ id” scope=“application” class=“…….” />

Javabeans Scopes page request session application valid until page completes request bean instance lasts for the client request. session bean lasts for the client session application bean instance created and lasts until application ends.

A Greeting Bean package beans; public class Greeting { ???.java package beans; public class Greeting { private String greeting; // the property public Greeting() { greeting = "Hello World"; } public String getGreeting() { return greeting; } public void setGreeting(String g) greeting = (g == null) ? "Hello World" : g; }

Java Beans Naming convention If the property name is greeting The get method must have the name: getGreeting The set method must have the name: setGreeting

Creating a Bean/1 Create a bean and use default property <jsp:useBean id="hello" class="beans.Greeting" /> Create a bean and set its property when it is constructed <jsp:setProperty name="hello" property="greeting“ value="Hello JSP World" /> </jsp:useBean> Here <jsp:setProperty> is in the body of the <jsp:useBean> element.

Creating a Bean/2 Create a bean and set its property after it has been constructed <jsp:useBean id="hello" class="beans.Greeting" /> <jsp:setProperty name="hello" property="greeting“ value="Hello JSP World" /> The <jsp:setProperty> tag is now outside the <jsp:useBean> tag, so it will always set the property, not just when the bean is constructed

Example 1: Using Java Beans in JSP Files <jsp:useBean id="hello" class="beans.Greeting" /> <jsp:setProperty name="hello" property="greeting" value="Hello JSP World" /> <html> <head> <title>Greeting JSP that uses a Greeting bean</title> </head> <body> <h1>Greeting JSP that uses a Greeting bean</h1> <p><jsp.getProperty name="hello" property="greeting" /> </p> </body> </html>

Example 2: Using Java Beans in JSP Files Two Beans: One initialized explicitly and the other is initialized using a request parameter ???.java <jsp:useBean id="greet1" class="beans.Greeting" /> <jsp:useBean id="greet2" class="beans.Greeting" /> <jsp:setProperty name="greet1" property="greeting" value="Hello JSP World" /> <jsp:setProperty name="greet2" property="greeting" param="greeting" /> <html><head><title>Greeting JSP using two Greeting beans</title></head><body> <h1>Greeting JSP using two Greeting beans</h1> <p>1st bean: <jsp:getProperty name="greet1" property="greeting" /></p> <p>2nd bean: <jsp:getProperty name="greet2" property="greeting" /></p></body></html>

Example 3: Using Java Beans in JSP Files Three Beans: One initialized explicitly, one is initialized using a request parameter, and one is initialized using getParameter ???.java <jsp:useBean id="greet1" class="beans.Greeting" /> <jsp:useBean id="greet2" class="beans.Greeting" /> <jsp:useBean id="greet3" class="beans.Greeting" /> <jsp:setProperty name="greet1" property="greeting" value="Hello JSP World" /> <jsp:setProperty name="greet2" property="greeting" param="greeting" /> <%-- Following works but param method is better --%> <jsp:setProperty name="greet3" property="greeting" value="<%= request.getParameter(\"greeting\") %>" />