/
Introduction to  Java  Bean Remember : JSP Standard  Actions Introduction to  Java  Bean Remember : JSP Standard  Actions

Introduction to Java Bean Remember : JSP Standard Actions - PowerPoint Presentation

marina-yarberry
marina-yarberry . @marina-yarberry
Follow
345 views
Uploaded On 2019-11-05

Introduction to Java Bean Remember : JSP Standard Actions - PPT Presentation

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 ID: 763503

bean jsp class greeting jsp bean greeting class beans usebean java property page scope setproperty request public greeting

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Introduction to Java Bean Remember : J..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


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 default constructor (no parameters) It should be Serializable (Implements the   java.io.Serializable ) It should provide methods to set and get the values of the properties, known as getter and setter methods Why use Java Bean ? I t 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    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. 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. It is the default scoperequest: 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-argument 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 package com.se432; public class Calculator { public int cube( int n ){ return n*n*n; } } <jsp:useBean id="obj" class="com.se432.Calculator“ />    <%   int m = obj.cube(5);   out.print("cube of 5 is "+m);  %>  Calculator.javaindex.jsp

Javabeans A Javabeans is a special type of the class that has a number of methods . The JSP page can call these methods so you 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, it sends the details to 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 valid until page completes request bean instance lasts for the client request.sessionbean lasts for the client sessionapplicationbean instance created and lasts until application ends.

A Greeting Bean 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

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:useBean id="hello" class="beans.Greeting" /> <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 < j sp: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>???.java

Example 2: Using Java Beans in JSP Files Two Beans: One initialized explicitly and the other is initialized using a request parameter < 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> ???.java

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<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\") %>" /> ???.java