Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Bean

Similar presentations


Presentation on theme: "Introduction to Java Bean"— Presentation transcript:

1 Introduction to Java Bean

2 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>

3 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? 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

4 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() );   } }  

5 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> 

6 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 scope 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-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.

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

8 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 . 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 s. This way, there would be no code in the JSP page dealing with sending s. To use a Javabean in a JSP page use the following syntax: <jsp:usebean id=“ id” scope=“application” class=“…….” />

9 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.

10 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;

11 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

12 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.

13 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

14 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>

15 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>

16 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\") %>" />


Download ppt "Introduction to Java Bean"

Similar presentations


Ads by Google