Introduction to Java Bean Remember JSP Standard Actions

  • Slides: 16
Download presentation
Introduction to Java Bean

Introduction to Java Bean

Remember: JSP Standard Actions • Standard actions are well known tags that affect the

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: use. Bean> <jsp: set. Property> <jsp: get. Property> <jsp: param> <jsp: include> <jsp: forward> <jsp: plugin>

Java Bean • A Java Bean is a java class that should following conventions:

Java Bean • A Java Bean is a java class that should 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 //To access the java bean class,

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

jsp: use. Bean action tag • The jsp: use. Bean action tag is used

jsp: use. Bean action tag • The jsp: use. Bean 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: use. Bean id= "instance. Name" scope= "page | request | session | application" class= "package. Name. class. Name" type= "package. Name. class. Name" bean. Name="package. Name. class. Name | <%= expression >" > </jsp: use. Bean>

Attributes and Usage • id: is used to identify the bean in the specified

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 bean. Name attribute. If you use it without class or bean. Name, no bean is instantiated. • bean. Name: instantiates the bean using the java. beans. Beans. instantiate() method.

Simple example of jsp: use. Bean action tag Calculator. java package com. javatpoint; public

Simple example of jsp: use. Bean action tag Calculator. java package com. javatpoint; public class Calculator{ public int cube(int n){return n*n*n; } } index. jsp <jsp: use. Bean 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

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 Java. Beans 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 1. page • valid until page completes 2. request • bean instance

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

A Greeting Bean ? ? ? . java package beans; public class Greeting {

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

Java Beans Naming convention If the property name is greeting The get method must

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

Creating a Bean/1 • Create a bean and use default property <jsp: use. Bean

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

Creating a Bean/2 • Create a bean and set its property after it has

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

Example 1: Using Java Beans in JSP Files ? ? ? . java <jsp:

Example 1: Using Java Beans in JSP Files ? ? ? . java <jsp: use. Bean id="hello" class="beans. Greeting" /> <jsp: set. Property name="hello" property="greeting" value="Hello JSP World" /> <html> <head> <title>Greeting JSP that uses a Greeting bean</title> </head> <body> <h 1>Greeting JSP that uses a Greeting bean</h 1> <p><jsp. get. Property name="hello" property="greeting" /> </p> </body> </html>

Example 2: Using Java Beans in JSP Files • Two Beans: One initialized explicitly

Example 2: Using Java Beans in JSP Files • Two Beans: One initialized explicitly and the other is initialized using a request parameter ? ? ? . java <jsp: use. Bean id="greet 1" class="beans. Greeting" /> <jsp: use. Bean id="greet 2" class="beans. Greeting" /> <jsp: set. Property name="greet 1" property="greeting" value="Hello JSP World" /> <jsp: set. Property name="greet 2" property="greeting" param="greeting" /> <html><head><title>Greeting JSP using two Greeting beans</title></head><body> <h 1>Greeting JSP using two Greeting beans</h 1> <p>1 st bean: <jsp: get. Property name="greet 1" property="greeting" /></p> <p>2 nd bean: <jsp: get. Property name="greet 2" property="greeting" /></p></body></html>

Example 3: Using Java Beans in JSP Files • Three Beans: One initialized explicitly,

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 get. Parameter ? ? ? . java <jsp: use. Bean id="greet 1" class="beans. Greeting" /> <jsp: use. Bean id="greet 2" class="beans. Greeting" /> <jsp: use. Bean id="greet 3" class="beans. Greeting" /> <jsp: set. Property name="greet 1" property="greeting" value="Hello JSP World" /> <jsp: set. Property name="greet 2" property="greeting" param="greeting" /> <%-- Following works but param method is better --%> <jsp: set. Property name="greet 3" property="greeting" value="<%= request. get. Parameter("greeting") %>" />